Regex

Understanding Regex with more than 60 examples

				
					//*******************************************REGEX*********************************************************
//Regular expression literal anything inside this/
let str1 = /a/;
//console.log(str1.source);
//output :- a

//---------------------------------------------------------------------------------------------------------

let search2 = /India/;
let str2 = "India is a beautiful country.Please visit India if you live in other country.";
//console.log(search2.exec(str2));
//output :- ['India', index: 0, input: 'India is a beautiful country.Please visit India if you live in other country.', groups: undefined]
//It searches only once .. India - the zero index. But there are two India in it

//---------------------------------------------------------------------------------------------------------

//g means global..
let search3 = /India/g;
let str3 = "India is a beautiful country.Please visit India if you live in other country.";
//console.log(search3.exec(str3)); //['India', index: 0, input: 'India is a beautiful country.Please visit India if you live in other country.', groups: undefined]
//console.log(search3.exec(str3)); //['India', index: 42, input: 'India is a beautiful country.Please visit India if you live in other country.', groups: undefined]
//console.log(search3.exec(str3)); //null

//---------------------------------------------------------------------------------------------------------

//it cannot find the case insensitive india.. all are null
let search4 = /india/g;
let str4 = "India is a beautiful country.Please visit India if you live in other country.";
console.log(search4.exec(str4)); //null
console.log(search4.exec(str4)); //null
console.log(search4.exec(str4)); //null

//---------------------------------------------------------------------------------------------------------

//it finds the small india
//i -->for case insensitivity
let search5 = /india/gi;
let str5 = "India is a beautiful country.Please visit India if you live in other country.";
console.log(search5.exec(str5)); //['India', index: 0, input: 'India is a beautiful country.Please visit India if you live in other country.', groups: undefined]
console.log(search5.exec(str5)); //['India', index: 42, input: 'India is a beautiful country.Please visit India if you live in other country.', groups: undefined]
console.log(search5.exec(str5)); //null

//---------------------------------------------------------------------------------------------------------

//it finds the small india
//result.input --> gives you the input string...
//result.index --> gives you the index of matched string where it starts
let search6 = /india/gi;
let str6 = "India is a beautiful /n country. /n Please visit India if you live in other country.";
var result6 = search6.exec(str6); //['India', index: 0, input: 'India is a beautiful country.Please visit India if you live in other country.', groups: undefined]
console.log(result6.input); //India is a beautiful /n country. /n Please visit India if you live in other country.
console.log(result6.index); //0

//---------------------------------------------------------------------------------------------------------

var result61 = search6.exec(str6); //['India', index: 0, input: 'India is a beautiful country.Please visit India if you live in other country.', groups: undefined]
console.log(result61.input); //India is a beautiful /n country. /n Please visit India if you live in other country.
console.log(result61.index); //49

//---------------------------------------------------------------------------------------------------------

//test --> returns true or false
let search7 = /india/gi;
let str7 = "India is a beautiful /n country. /n Please visit India if you live in other country.";
var result7 = search7.test(str7);
console.log(result7); //true

//---------------------------------------------------------------------------------------------------------

//match --> returns array of results or null
let search8 = /india/gi;
let str8 = "India is a beautiful /n country. /n Please visit India if you live in other country.";
var result8 = str8.match(search8);
console.log(result8); //['India', 'India']

//---------------------------------------------------------------------------------------------------------

//match --> returns the matched elements...
let search9 = /india/gi;
let str9 = "India is a beautiful /n country. /n Please visit India if you live in other country.";
var result9 = str9.search(search9);
console.log(result9); //0 --> index 0 is returned..

//---------------------------------------------------------------------------------------------------------

//replace --> returns the matched elements...
let search10 = /india/gi;
let str10 = "India is a beautiful /n country. /n Please visit India if you live in other country.";
var result10 = str10.replace(search10, "My India");
console.log(result10); //My India is a beautiful /n country. /n Please visit My India if you live in other country.

//---------------------------------------------------------------------------------------------------------

//^ --> Expression will match if it start with...yes it starts with India/india --> true
let search11 = /^india/gi;
let str11 = "India is a beautiful /n country. /n Please visit India if you live in other country.";
var result11 = search11.test(str11); //true
console.log(result11);

//---------------------------------------------------------------------------------------------------------

//^ --> Expression will match if it start with...yes it starts with India/india --> true
let search12 = /india$/gi;
let str12 = "India is a beautiful /n country. /n Please visit India if you live in other country.";
var result12 = search12.test(str12);
console.log(result12); //false

//---------------------------------------------------------------------------------------------------------

//. --> Matches any one character...
let search13 = /in.dia/gi;
let str13 = "Inxdia is a beautiful.";
var result13 = search13.test(str13);
console.log(result13); //true

//---------------------------------------------------------------------------------------------------------

//. --> Matches any one character...
let search14 = /in.dia/gi;
let str14 = "Inxxxxxxxxxxxxxxdia is a beautiful.";
var result14 = search14.test(str14);
console.log(result14); //false

//---------------------------------------------------------------------------------------------------------

//* --> Matches any 0 or more character
let search15 = /h*rry/;
let str15 = "srry"; //true
var result15 = search15.test(str15);
console.log("result15", result15);

//---------------------------------------------------------------------------------------------------------

//* --> Matches any 0 or more character
//bo -->b followed by o or more than one o's -->but it has to be followed by ttt after o or or o's
let search16 = /bo*ttt/;
//let str16 = "bottt";//true
//let str16 = "bott"; //false
//let str16 = "bobobobottt"; //true
//let str16 = "sadsadsdasdssadbottt"; //true
//let str16 = "booooooooooooooorrrttt"; //false
//let str16 = "booooooooooooooottt"; //true
//let str16 = "ttt"; //false
let str16 = "ottt"; //false
var result16 = search16.test(str16);
console.log("result16", result16);

//---------------------------------------------------------------------------------------------------------
//? --> n is optional
//Idia will match ...
let search17 = /In?dia/;
//let str17 = "Idia"; //true
let str17 = "Ixdia"; //false ..a is optional,,that doesnt mean you can have any other letter like x..
var result17 = search17.test(str17);
console.log("result17", result17);

//---------------------------------------------------------------------------------------------------------

//? --> n is optional
let search18 = /In?dia?/;
let str18 = "Idit"; //true ..last a is optional ...we are matching till In?di ...which is fine so true...
var result18 = search18.test(str18);
console.log("result18", result18);

//---------------------------------------------------------------------------------------------------------

//? --> n is optional
let search19 = /In?dia?n/;
let str19 = "Idit"; //false ..last a is optional ...Idin will make it true but Idit will be false...
var result19 = search19.test(str19);
console.log("result19", result19);

//---------------------------------------------------------------------------------------------------------

//****character set **************/
//[] --> character set ...
let search20 = /In[a-z]dia/; //can be any character between a to z
let str20 = "Inxdia"; //true
//let str20 = "Inxssasasadia"; //false
var result20 = search20.test(str20);
console.log("result20", result20);

//---------------------------------------------------------------------------------------------------------

//[] --> character set ...
let search21 = /In[abc]dia/; //can be either a or b or c
//let str21 = "Incdia"; //true
let str21 = "Inabcdia"; //false ..as it can be either a b or c
var result21 = search21.test(str21);
console.log("result21", result21);

//---------------------------------------------------------------------------------------------------------

//[] --> character set ...
let search22 = /In[^abc]dia/; //can't be either a or b or c
let str22 = "Inadia"; //false ..as it can't be either a b or c
//let str22 = "Inxdia"; //true
var result22 = search22.test(str22);
console.log("result22", result22);

//---------------------------------------------------------------------------------------------------------

//[] --> character set ...
let search23 = /In[a-z]dia[0-9XTY]/; //In -->a to z-->dia --> any digit 0-9 or X or T or Y
let str23 = "InadiaX"; //true
var result23 = search23.test(str23);
console.log("result23", result23);

//---------------------------------------------------------------------------------------------------------

//[] --> character set ...
let search24 = /fo{2}tball/; //o can occur exactly two times
let str24 = "football"; //true
var result24 = search24.test(str24);
console.log("result24", result24);

//---------------------------------------------------------------------------------------------------------

//[] --> character set ...
let search25 = /fo{0,4}tball/; //o can occur 0 or 1 or 2 or 3 or 4 times
//let str25 = "ftball"; //true
//let str25 = "fotball"; //true
//let str25 = "football"; //true
//let str25 = "foootball"; //true
//let str25 = "fooootball"; //true
let str25 = "foooootball"; //false
var result25 = search25.test(str25);
console.log("result25", result25);

//---------------------------------------------------------------------------------------------------------

// --> minimum 4 a
let search26 = /a{4,}/; //minimum 4 a's so its true...
let str26 = "aaasssaaaaaaa"; //true
var result26 = search26.test(str26);
console.log("result26", result26);

//---------------------------------------------------------------------------------------------------------

//one character between a and c ....but it gives you entire string
let search27 = /a.+c/g;
let str27 = "a1cdssfdsfdsfdsfa1saasc";
var result27 = str27.match(search27);
console.log("result27", result27); //['a1cdssfdsfdsfdsfa1saasc']

//---------------------------------------------------------------------------------------------------------

//one character between a and c ....but it gives you entire string
let search29 = /a.c/g;
let str29 = "a1cdssfdsfdsfdsfa1c";
var result29 = str29.match(search29);
console.log("result29", result29); //['a1c', 'a1c']

//---------------------------------------------------------------------------------------------------------

//one character between a and c ....but there are two matches ...when you put ? it works gives
//you two strings
let search28 = /a.+?c/g;
let str28 = "a1cdssfdsfdsfdsfa1c";
var result28 = str28.match(search28);
console.log("result28", result28); //['a1c', 'a1c']

//---------------------------------------------------------------------------------------------------------

//gives me all digits
let search30 = /\d/g;
let str30 = "123asas2178931sasas";
var result30 = str30.match(search30);
console.log("result30", result30); //['1', '2', '3', '2', '1', '7', '8', '9', '3', '1']

//---------------------------------------------------------------------------------------------------------

//gives me all except digits
let search31 = /\D/g;
let str31 = "123asas 2178931sasas";
var result31 = str31.match(search31);
console.log("result31", result31); //['a', 's', 'a', 's', ' ', 's', 'a', 's', 'a', 's']

//---------------------------------------------------------------------------------------------------------

//-->\w matches any single letter, number or underscore
//-->\W just opposite of \w
let search32 = /\W/g;
let str32 = "123asas 2178931sasas";
var result32 = str32.match(search32);
console.log("result32", result32); //[' ']

//---------------------------------------------------------------------------------------------------------

//\w --> matches any single letter, number or underscore
let search33 = /\w/g;
let str33 = "123asas 2178931sasas";
var result33 = str33.match(search33);
console.log("result33", result33); //['1', '2', '3', 'a', 's', 'a', 's', '2', '1', '7', '8', '9', '3', '1', 's', 'a', 's', 'a', 's']

//---------------------------------------------------------------------------------------------------------

//\s stands for “whitespace character”
let search34 = /\s/g;
let str34 = "123asas 2178931 sasas";
var result34 = str34.match(search34);
console.log("result34", result34); //[' ', ' ']

//---------------------------------------------------------------------------------------------------------

//\s stands for “whitespace character”
let search35 = /\S/g;
let str35 = "123asas 2178931 sasas";
var result35 = str35.match(search35);
console.log("result35", result35); //['1', '2', '3', 'a', 's', 'a', 's', '2', '1', '7', '8', '9', '3', '1', 's', 'a', 's', 'a', 's']

//---------------------------------------------------------------------------------------------------------

// date --> 23-08-2022
let search36 = /\d\d-\d\d-\d\d\d\d/g;
let str36 = "23-028-2022"; //null
str36 = "23-08-2022"; //['23-08-2022']
var result36 = str36.match(search36);
result36 = search36.test(str36);
console.log("result36", result36); //true

{
  /* <input type="text" pattern="\d\d-\d\d-\d\d\d\d"/>
input[type="text"]:invaid {
  color:red
} */
}

//---------------------------------------------------------------------------------------------------------

//91 is common --> followed by 89 or 79
let search37 = /91(89 |79)/g;
let str37 = "9189 9179";
var result37 = str37.match(search37);
result37 = search37.test(str37);
console.log("result37", result37); //true

//---------------------------------------------------------------------------------------------------------

//Replace - coding is awesome --> javascript is awesome
let search38 = /coding is (?=awesome)/g;
let str38 = "coding is awesome.coding is awesome!!";
var result38 = str38.replace(search38, "Javascript is ");
console.log("result38", result38); //Javascript is awesome.Javascript is awesome!!

//---------------------------------------------------------------------------------------------------------

//Replace - coding is awesome --> javascript is awesome
let search39 = /o/g;
let str39 = "In my free time I watch football.. Football is awesome.!!";
let result39 = search39.test(str39); //true
result39 = str39.match(search39); //['o', 'o', 'o', 'o', 'o']
console.log("result39", result39); //true
//---------------------------------------------------------------------------------------------------------

let search40 = /o+/g;
let str40 = "In my free time I watch football.. Football is awesome.!!";
let result40 = search40.test(str40); //true
result40 = str40.match(search40);
console.log("result40", result40); //['oo', 'oo', 'o']
//---------------------------------------------------------------------------------------------------------

//?is optional ...
let search41 = /ot?/g;
let str41 = "In my free time I watch football.. Football is awesome.!!";
let result41 = search41.test(str41); //true
result41 = str41.match(search41);
console.log("result41", result41); // ['o', 'ot', 'o', 'ot', 'o']
//---------------------------------------------------------------------------------------------------------

//*
//it will match anything that is r + anything that is with re or ree (r followed by any e)
let search42 = /re*/g;
let str42 = "There is a tree outside. I want to roam in my free time. Super climate!!";
let result42 = search42.test(str42); //true
result42 = str42.match(search42);
console.log("result42", result42); // ['re', 'ree', 'r', 'ree', 'r']
//---------------------------------------------------------------------------------------------------------

//.
//find letter just before fullstop
let search43 = /.\./g;
let str43 = "There is a tree outside. \n I want to roam in my free time. Super climate!!";
let result43 = search43.test(str43); //true
result43 = str43.match(search43);
console.log("result43", result43); // ['e.', 'e.']
//---------------------------------------------------------------------------------------------------------

//.
//find all min 4 letter words...
let search44 = /\w{4,}/g;
let str44 = "There is a tree outside. \n I want to roam in my free time. Super climate!!";
let result44 = search44.test(str44); //true
result44 = str44.match(search44);
console.log("result44", result44); // ['There', 'tree', 'outside', 'want', 'roam', 'free', 'time', 'Super', 'climate']
//---------------------------------------------------------------------------------------------------------

//Grouping to find the or The
let search45 = /(t|T)he/g;
let str45 = "The Test cricket is awesome, but the test cricket is loosing its charm !!";
let result45 = search45.test(str45); //true
result45 = str45.match(search45);
console.log("result45", result45); // ['The', 'the']
//---------------------------------------------------------------------------------------------------------

//matches 2 or 3 character just before the full stop. that too if it finds t e or r
let search46 = /(t|e|r){2,3}\./g;
//search46 = /(t|e|r){2,3}/g;//['tre', 'et']
let str46 = "This is Nehru Nagar street.";
let result46 = search46.test(str46); //true
result46 = str46.match(search46);
console.log("result46", result46); // ['eet.']
//---------------------------------------------------------------------------------------------------------

//()--> Groupings...
let search47 = /(Ind){3}/; //Ind occuring 3 times or more
let str47 = "IndIndIndIndiaaa"; //true
var result47 = search47.test(str47);
console.log("result47", result47);
//---------------------------------------------------------------------------------------------------------

//Look Behind
//After the or The ...
let search48 = /(?<=[Tt]he)./g;
let str48 = "The Test cricket is awesome, but the test cricket is loosing its charm,the@@ !!";
let result48 = search48.test(str48); //true
result48 = str48.match(search48);
console.log("result48", result48); // [' ', ' ', '@']
//---------------------------------------------------------------------------------------------------------

//Anything that doesnt have the word the before
//you dont get the space after the ...
let search49 = /(?<![Tt]he)./g;
let str49 = "wow The cricket";
let result49 = search49.test(str49); //true
result49 = str49.match(search49);
console.log("result49", result49); // ['w', 'o', 'w', ' ', 'T', 'h', 'e', 'c', 'r', 'i', 'c', 'k', 'e', 't']
//---------------------------------------------------------------------------------------------------------

//Look Ahead
//look for anything before cricket or Cricket
let search50 = /.(?=[Cc]ricket)/g;
let str50 = "wow The @cricket $Cricket";
let result50 = search50.test(str50); //true
result50 = str50.match(search50);
console.log("result50", result50); // ['@', '$']
//---------------------------------------------------------------------------------------------------------

//Look Ahead
//look for anything not followed by cricket or Cricket
let search51 = /.(?![Cc]ricket)/g;
let str51 = "wow The @cricket $Cricket";
let result51 = search51.test(str51); //true
result51 = str51.match(search51);
console.log("result51", result51); // ['w', 'o', 'w', ' ', 'T', 'h', 'e', ' ', 'c', 'r', 'i', 'c', 'k', 'e', 't', ' ', 'C', 'r', 'i', 'c', 'k', 'e', 't']
//---------------------------------------------------------------------------------------------------------

//check phone number 10 digits-- startightforward
let search52 = /\d{10}/g;
let str52 = "1234567890";
let result52 = search52.test(str52); //true
result52 = str52.match(search52);
console.log("result52", result52); //['1234567890']
//---------------------------------------------------------------------------------------------------------

//it should give true if it matches either of it
//1234567890 or 123-456-7890
//for both we should get true
//-? is optional ...
let search53 = /\d{3}-?\d{3}-?\d{4}/g;
let str53 = "1234567890";
str53 = "123-456-7890";
let result53 = search53.test(str53); //true
result53 = str53.match(search53);
console.log("result53", result53); //['123-456-7890']
//---------------------------------------------------------------------------------------------------------

//now it should also support
//123 456 7890
let search54 = /\d{3}[ -]?\d{3}[ -]?\d{4}/g;
let str54 = "123 456 7890";
let result54 = search54.test(str54); //true
result54 = str54.match(search54);
console.log("result54", result54); //['123 456 7890']
//---------------------------------------------------------------------------------------------------------

//Group the digits...
//let result551 = str55.replace(search55, "$1$2$3"); //1234567890
//you are able to replace spaces with numbers to --> 1234567890
//$1 is 123
//$2 is 456
//$3 is 7890
let search55 = /(\d{3})[ -]?(\d{3})[ -]?(\d{4})/g;
let str55 = "123 456 7890";
let result55 = search55.test(str55); //true
let result551 = str55.replace(search55, "$1$2$3"); //1234567890
console.log(result551);
result55 = str55.match(search55);
console.log("result55", result55); //['123 456 7890']
//---------------------------------------------------------------------------------------------------------

//Group the digits...
//let result561 = str55.replace(search56, "$<NdcCode>$<MiddleNum>$<LastNum>"); //1234567890
//you are able to replace hyphen with numbers to --> 1234567890
//$1 is now replaced by name -->?<NdcCode>
//$2 is replaced by name -->?<MiddleNum>
//$3 is replaced by name -->?<LastNum>
//naming helps to do replace
let search56 = /(?<NdcCode>\d{3})[ -]?(?<MiddleNum>\d{3})[ -]?(?<LastNum>\d{4})/g;
let str56 = "123-456-7890";
let result56 = search55.test(str56); //true
let result561 = str55.replace(search56, "$<NdcCode>$<MiddleNum>$<LastNum>"); //1234567890
console.log(result561); //1234567890
result56 = str56.match(search56);
console.log("result56", result56); //['123-456-7890']
//---------------------------------------------------------------------------------------------------------

//Group the digits...
//support one more variant of number --> (123) 456-7890
///\(?(?<NdcCode>\d{3})\)?
// you need to escape for paranthesis -- put \(?  and \)? at the end ,,thats it
let search57 = /\(?(?<NdcCode>\d{3})\)?[ -]?(?<MiddleNum>\d{3})[ -]?(?<LastNum>\d{4})/g;
let str57 = "(123) 456-7890";
let result57 = search57.test(str57); //true
let result571 = str57.replace(search56, "$<NdcCode>$<MiddleNum>$<LastNum>"); //1234567890
console.log(result571); //1234567890
result57 = str57.match(search57);
console.log("result57", result57); //['(123) 456-7890']
//---------------------------------------------------------------------------------------------------------

//Group the digits...
//support one more variant of number --> +1 123 456 7890
//All you need to add is --> (\+1[ -])? --> + is special character ..you need to escape for it
//and this +1 is optional so ? is needed...
let search58 = /(\+1[ -])?\(?(?<NdcCode>\d{3})\)?[ -]?(?<MiddleNum>\d{3})[ -]?(?<LastNum>\d{4})/g;
let str58 = "+1 123 456 7890";
let result58 = search58.test(str58); //true
let result581 = str58.replace(search56, "$<NdcCode>$<MiddleNum>$<LastNum>"); //1234567890
console.log(result581); //1234567890
result58 = str58.match(search58);
console.log("result58", result58); //['+1 123 456 7890']
//---------------------------------------------------------------------------------------------------------

//username with a-zA-Z -minimum 3 letters- max 10 letters
//must start with alphabet
//rest all character should be 3 to 10 ...it should not include first character...
//numbers allowed
//problem with this is as it doesnt end with $ ,,,{3,10} doesnt work
let search59 = /^[A-Za-z]([0-9a-zA-Z]){3,10}/g;
let str59 = "anurag"; //matched
str59 = "Anurag"; //matched
str59 = "8dfsf"; //no match found!!
str59 = "sd"; //no match found!!
str59 = "anurqwqwqwwqwqwqwqwqwqwq"; //matched ...this will give you true and doesnt check for length 10
//you have to end the pattern with $ to make it work
if (search59.test(str59)) {
  console.log("str59::", "matched");
} else {
  console.log("str59::", "no match found!!");
}
//---------------------------------------------------------------------------------------------------------

//Pattern ending with $
//username with a-zA-Z -minimum 3 letters- max 5 letters
//rest all character should be 3 to 5 ...it should not include first character...
//must start with alphabet
//numbers allowed
let search60 = /^[A-Za-z]([0-9a-zA-Z]){3,5}$/g;
let str60 = "anurag"; //matched
str60 = "Anurag"; //matched
str60 = "8dfsf"; //no match found!!
str60 = "sd"; //no match found!!
str60 = "anurqwqwqwwqwqwqwqwqwqwq"; //no match found!!
str60 = "A12345"; //matched!!
str60 = "A123456"; //no match found!!
str60 = "A12"; //no match found!!
str60 = "A123"; //matched!!
if (search60.test(str60)) {
  console.log("str60::", "matched");
} else {
  console.log("str60::", "no match found!!");
}
//---------------------------------------------------------------------------------------------------------

//Number should be 10 digit exactly
let search61 = /^([0-9]){10}$/g;
search61 = /^([0-9]){10}$/g;
let str61 = "1234567890"; //matched
//str61 = "233424"; //no match found!

if (search61.test(str61)) {
  console.log("str61::", "matched");
} else {
  console.log("str61::", "no match found!");
}
//---------------------------------------------------------------------------------------------------------

//Email Validation
//[_\-\.0-9a-zA-Z]+
//+ says you can have either number or a-z or A-Z
//without + it will be one character --> num or a-z or A-Z --> a or A or 1
//with + you can have multiple character  --> 1asdaAA_

let search62 = /^()@()\.()$/g;
search62 = /^([_\-\.0-9a-zA-Z]+)@([_\-\.0-9a-zA-Z]+)\.([a-zA-Z]){2,7}$/g;
let str62 = "anurag@gmail.com"; //matched
str62 = "anurag@gmail.co.in"; //matched
str62 = "anurag@gmail.comcomco"; //no match found!
str62 = "anurag@gmail.comcomc"; //matched --7 character
str62 = "_anurag@gmail.comcomc"; //matched --start with _
str62 = "12345@gmail.comcomc"; //matched --numbers allowed
//str62 = "233424"; //no match found!

if (search62.test(str62)) {
  console.log("str62::", "matched");
} else {
  console.log("str62::", "no match found!");
}

//----------------------------------THE END -------------------------------------------------------

				
			

Leave a Comment