Categories
ECMAScript Javascript

Regular Expression

/**
 * to demonstrate regEx
 */

//Attributes explaination
// G- match globally
// I - case insensitive, ignore matching of case.
// M - multiline match
// U - consider filter pattern as Unicode characters
// Y - start match from given fromIndex value, dont match globally

//meta characters to help build expressions
//.  any single character
//\s whitespace characters(space, tab, newline)
//\S non white space character
//\d a digit between 0 to 9
//\w a word character (a to z, A to Z, 0 to 9, _)
//\W non word character
//[\b] backspace character
//(a|b|c) matches a or b or c


//bracket Expressions explaination
//[...] any character in bracket matches
//[^...] no character in bracket matches
//[0-9] matches any digit between 0 to 9
//[a-z] matches any alphabet between a to z in lower case
//[A-Z] matches any alphabet between A to Z in upper case
//[a-Z] matches any alphabet between a to Z in any case
//[^a-zA-Z] matches any string that doesnt contain any character between a to z and A to Z.
//

//Quantifier expression explanation : we will explain with help of character a
//a+ matches any string that contains at least one occurance of a
//a* matches any string that contains zero or more occurance of a
//a? matches any string that contains one occurance of a
//a{N} matches any string that contains N number of occurances of a
//a{2,3} matches any string that contains sequences of 2 or 3 a
//a{2,} matches any string that contains sequences of 2 or more a
//p$ match string with p as end
//^p match string with p at beginning
//a.a match string which starts and ends with a with one character in between
//^.{2}$ matches any string which contains only 2 characters
//

(.*)

matches html paragraph with any string within it //A(WS)* - matches string starting with A and followed by any number of sequences of WS. //RegEx Properties and methods //RegExpObject.global to indicate if pattern matching is Global in scope var expr1 = new RegExp("AWS","g"); console.log("is pattern matching with global ?"+expr1.global); //RegExpObject.igonreCase to indicate if pattern matching is case insensitive var expr2 = new RegExp("AWS","i"); console.log("is pattern matching case insensitive expr2?"+expr2.ignoreCase); var expr3 = new RegExp("AWS"); console.log("is pattern matching case insensitive now expr3?"+expr3.ignoreCase); //RegExpObject.lastIndex - to return endIndex of last match var expr4 = new RegExp("cloud","gi"); console.log("is pattern matching case insensitive epxr4 :"+expr4.ignoreCase); console.log("is pattern matching case global epxr4 :"+expr4.global); var str1 = "AWS cloud is most popular cloud system. cloud reduces cost." do{ expr4.test(str1); console.log("current lastIndex expr4 : " + expr4.lastIndex); }while(expr4.lastIndex>0); //RegExpObject.multiline - to return true if matching to perform on multiple lines is set var expr5 = new RegExp("cloud","m"); console.log("is multiline matching set expr5 :"+expr5.multiline); //RegExpObject.source - text pattern to match var expr6 = new RegExp("A(WS)*"); console.log("source text pattern to match expr6 : "+expr6.source + ", "+expr6.test("AWSWSWS")); //RegExpObject.exec() - search for pattern and return matching array of results. if no match returns NULL var expr7 = new RegExp("cloud","gi"); var str2 = "AWS CLOUD is most popular cloud system, to reduce cost cloud is future"; console.log("exec for cloud ::"+expr7.exec(str2)); console.log("exec for cloud ::"+expr7.exec(str2)); //RegExpObject.test() - search for pattern and return TRUE if matching expression found else FALSE. var expr7 = new RegExp("cloud","gi"); var str2 = "AWS CLOUD is most popular cloud system, to reduce cost cloud is future"; console.log("exec for cloud ::"+expr7.test(str2)); console.log("exec for cloud ::"+expr7.test(str2)); //RegExpObject.replace(regExpObject|string, newString|function) //replaces matching regExpObject pattern or string with newString or new string generated by function var expr8 = new RegExp("cloud"); var expr9 = new RegExp("cloud","gi"); var str3 = "AWS CLOUD is most popular cloud system, to reduce cost cloud is future"; console.log("replace cloud by host expr8 ::"+str3.replace(expr8,"host")); console.log("replace cloud by host expr9 ::"+str3.replace(expr9,"host")); //RegExpObject.search(pattern) - search and return index where found var str4 = "AWS CLOUD is most popular cloud system, to reduce cost cloud is future"; var expr10 = new RegExp(/cloud/gi); console.log("search for cloud :: "+ str4.search(expr10)); //RegExpObject.split([separator,limit]) - to split by separator, limit is maximum number of splits var str5 = "AWS CLOUD is most popular cloud system, to reduce cost cloud is future"; console.log("split str5 by space:: " + str5.split(" ")); console.log("split str5 by cloud:: " + str5.split(/cloud/gi)); console.log("split str5 by cloud for 2 :: " + str5.split(/cloud/gi,2)); //RegExpObject.toString() - to return string equivalent console.log("expr9 ::" +expr9.toString());

Output

Leave a comment

Design a site like this with WordPress.com
Get started