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))
{
   alert("m");
}


compile(): change pattern

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

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:

Modifier
Description
i
case insensitive search
g
find all matches rather than 1
m
multiline matching


Regular Expression Syntax:

Syntax
Description
\d
Digit, 0,1,2 ... 9
\D
Not Digit
\s
Space
\S
Not Space
\w
Word
\W
Not Word
\t
Tab
\n
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
[ab]
a or b
[^ab]
Any character except a and b
[0-9]
All Digit
[A-Z]
All uppercase A to Z letters
[a-z]
All lowercase a to z letters
[A-z]
All Uppercase and lowercase a to z letters
i+
i at least one time
i*
i zero or more times
i?
i zero or 1 time
i{n}
i occurs n times in sequence
i{n1,n2}
i occurs n1 - n2 times in sequence
i{n,}
i occures >= n times
\0
NUL
\f
Form feed character
\r
Carriage return character
\v
Vertical tab
\xhhhh
Unicode with 4 characters of hexadecimal code hhhh
\xhh
Character with 2 characters of hexadecimal code hh
?=i
Lookahead matches only if i is followed
?!i
Lookahead matches only if i is not followed
endmemo.com © 2024  | Terms of Use | Privacy | Home