JS Regular Expression Tutorials

Regular Expression object initialization:

var reg = new RegExp(pattern, modifier);
var reg = /pattern/modifier;

String has some methods that support regular expression, include match(), replace(), search().

match() method: regular expression pattern match, return matches:

var s = "endmemo.com";
var m = s.match(/.m/g); //m is an array returned
m.valueOf(); //dm,em,om
m = s.match(/nd(.+)o\./);
alert(m[0]); //ndmemo.
alert(m[1]); //mem, matched pattern in ()

replace() method: replace a substring with new, supports regular expression:

var s = "endmemo.com R language tutorial";
var x = s.replace("R language","javascript");
//x is "endmemo.com javascript tutorial"
var x = s.replace(/\sR.+ge\s/,"javascript");
//same result, using regular expression
//using $1, $2 to replace the matches in brackets
var x = s.replace(/\sR(.+ge)\s/," javascript$1 ");
//endmemo.com javascript language tutorial
//$0 is the whole match of the pattern
var x2 = s.replace(/\sR(.+ge)\s/,"javascript$0");
//endmemo.com javascript R language tutorial

search() method: return the occurrence position of a substring or regular expression pattern:

var s = "endmemo.com";
var p = s.search("dm"); //p is 2
var p = s.search(/e.o/); //p is 4
var p = s.search(/\s\d/); //p is -1, not found

Methods of javascript RegExp object include:

exec(): find the first match of a pattern

var s = "endmemo.com";
var reg=/\.c/;
var result = reg.exec(s);
alert(result); //.c

test(): test the match of a pattern, return true or false

var s = "endmemo.com";
var reg=/\.c/;
var result = reg.test(s);
alert(result); true

Use variables in string match

var str = "endmemo.com";
var pat= new RegExp("me","g");
if (str.match(pat))

compile(): change pattern

var s = "endmemo.com";
var reg=/\.c/;
var result = s.match(reg);
alert(result[0]); //true
result = s.match(reg);

compile() method is faster if you have multiple searches and need to change the pattern.

global property: has "g" modifier or not
ignoreCase property: has "i" modifier or not
multiline property: has "m" modifier or not
lastIndex property: position to start the next match
source property: regular expression pattern text

var reg = /^\dend/ig;
alert(reg.global); //true
alert(reg.multiline); //false

Regular Expression Modifiers:

case insensitive search
find all matches rather than 1
multiline matching

Regular Expression Syntax:

Digit, 0,1,2 ... 9
Not Digit
Not Space
Not Word
New line
Beginning of the string
End of the string
Escape special characters, e.g. \\ is "\", \+ is "+"
Alternation match. e.g. /(e|d)n/ matches "en" and "dn"
Any character, except \n or line terminator
a or b
Any character except a and b
All Digit
All uppercase A to Z letters
All lowercase a to z letters
All Uppercase and lowercase a to z letters
i at least one time
i zero or more times
i zero or 1 time
i occurs n times in sequence
i occurs n1 - n2 times in sequence
i occures >= n times
Form feed character
Carriage return character
Vertical tab
Unicode with 4 characters of hexadecimal code hhhh
Character with 2 characters of hexadecimal code hh
Lookahead matches only if i is followed
Lookahead matches only if i is not followed
endmemo.com © 2024  | Terms of Use | Privacy | Home