JavaScript Regular Expressions Part2 - JavaScript Tutorial 89

Published: 22 January 2022
on channel: ChidresTechTutorials
2,137
41

Notes for You:: JavaScript Regular Expressions Part2 - JavaScript Tutorial 89
JavaScript RegExp Object - using flags:
Sequence of characters enclosed in between pair of forward slash symbols indicates a regular expression in JavaScript.

Syntax for defining a variable of type regular expression is:
var variableName = /search pattern/ [flags] ;

Ex: validating a phone number
var pattern = / ^[0-9]{10}$ /;

Flags:
indicate how a regular expression must be interpreted.

Ex:
i: ignore case
indicates ignore the difference between lower case characters and upper case characters.

g: global match
indicates find all occurrences of the pattern rather than stopping at the first occurrence.



Code to understand i flag:

/*
var str = "Agent007";
var pattern = /Agent/;
var rArray = [];

rArray = pattern.exec(str); // ["Agent"]
d.w("found word: ", rArray[0], "<br/>"); // Agent
d.w("location: ", rArray.index, "<br/>"); // 0
*/


/*
var str = "Agent007";
var pattern = /agent/;
var rArray = [];

rArray = pattern.exec(str); // null
d.w("found word: ", rArray[0], "<br/>"); // error
d.w("location: ", rArray.index, "<br/>"); // error
*/


var str = "Agent007";
var pattern = /agent/i;
var rArray = [];

rArray = pattern.exec(str); // ["Agent"]
d.w("found word: ", rArray[0], "<br/>"); // Agent
d.w("location: ", rArray.index, "<br/>");// 0



Code to understand g flag:

/*
var str = "Agent007 is the best agent";
var pattern = /agent/i;
var rArray = [];

rArray = pattern.exec(str); // ["Agent"]
d.w("found word: ", rArray[0], "<br/>"); // Agent
d.w("location: ", rArray.index, "<br/>"); // 0

rArray = pattern.exec(str); // ["Agent"]
d.w("found word: ", rArray[0], "<br/>"); // Agent
d.w("location: ", rArray.index, "<br/>"); // 0
*/

var str = "Agent007 is the best agent";
var pattern = /agent/ig;
var rArray = [];

/*
rArray = pattern.exec(str); // ["Agent"]
d.w("found word: ", rArray[0], "<br/>"); // Agent
d.w("location: ", rArray.index, "<br/>"); // 0

rArray = pattern.exec(str); // ["agent"]
d.w("found word: ", rArray[0], "<br/>"); // agent
d.w("location: ", rArray.index, "<br/>"); // 21

rArray = pattern.exec(str); // null
d.w("found word: ", rArray[0], "<br/>"); // error
d.w("location: ", rArray.index, "<br/>"); // error
*/

// OR

while( (rArray = pattern.exec(str)) != null ){
d.w("found word: ", rArray[0], "<br/>");
d.w("location: ", rArray.index, "<br/>");
}


Note:
replace d.w with document.write.
replace < with less-than symbol.
replace > with greater-than symbol.


=========================================

Follow the link for next video:
JavaScript Tutorial 90 - Regular Expression Meta Characters in JavaScript
   • JavaScript Regular Expressions Part3 ...  

Follow the link for previous video:
JavaScript Tutorial 88 - JavaScript RegExp Object
   • JavaScript Regular Expressions Part1 ...  

=========================================

JavaScript Tutorials Playlist:-
   • JavaScript Tutorials  

=========================================
Watch My Other Useful Tutorials:-

jQuery Tutorials Playlist:-
   • jQuery Tutorials  

jQuery UI Tutorials Playlist:-
   • jQuery UI Tutorials  

Bootstrap Tutorials Playlist:-
   • Bootstrap4 Tutorials  

=========================================

Subscribe to our YouTube channel:-
   / chidrestechtutorials  

Join as member of our YouTube channel:-
https://www.youtube.com/chidrestechtu...

Become our Patron:-
  / chidrestechtutorials  

Visit our Website:-
https://www.chidrestechtutorials.com

Download our Notes from Instamojo:-
https://chidrestechtutorials.myinstam...

Buy our Products on Spring:-
https://chidres-tech-tutorials.creato...

=========================================
Follow us:-

Google My Business:-
https://chidrestechtutorials.business...

Google Blog:-
http://manjunathchidre.blogspot.com

LinkedIn:-
  / chidrestechtutorials  

Facebook:-
  / chidrestechtutorials  

Twitter:-
  / manjunathchidre  

Tumblr:-
  / chidrestechtutorials  

Pinterest:-
  / chidrestechtutorials  

=========================================

Despite my inconsistent uploads; Thanks for being amazing learners and still sticking with me on YouTube.

=========================================
Hash Tags:-
#ChidresTechTutorials #JavaScript #JavaScriptTutorial


On this page of the site you can watch the video online JavaScript Regular Expressions Part2 - JavaScript Tutorial 89 with a duration of hours minute second in good quality, which was uploaded by the user ChidresTechTutorials 22 January 2022, share the link with friends and acquaintances, this video has already been watched 2,137 times on youtube and it was liked by 41 viewers. Enjoy your viewing!