1. Matching pattern tag:
g ------- represents the global pattern, instead of ending immediately when the first successful match is found
i ------- represents the case-insensitive pattern
m ----- means multi-line matching. When one line ends, continue matching to the next line
//Match the first bat or cat, case-insensitive
var pattern1 = /[bc]at/i
var pattern2 = new RegExp("[bc]at" ,"i"); //Same as above
Use RegEXp to convert the literal mode into a string
/[bc]at/ =======> "\[ bc\]at"
/.at/ ==========> "\.at"
/name/age/ ======> "name\/age "
/d.d{1,2}/ ======> "\d.\d{1,2}"
/w\hello\123/ ====>"\ w\\hello\\123
2.RegExp instance attribute
global - Boolean value, indicating whether the g flag is set
ignoreCase - Boolean value, indicating whether the i flag is set
multiline - Boolean value, indicating whether the m flag is set
lastIndex - integer, indicating the position to start searching for the next match
source - string in literal form
var pattern2 = new RegExp("\[bc\]at","i");
alert(pattern2.source); //"[bc]at"
3.RegExp instance method
exec() method accepts one parameter, which is the character to which the regular expression is applied String,
returns an array and two attributes index and input. The first item in the array is the string matching the entire pattern, and the other items are strings matching the capturing groups in the pattern
var text "mom and dat and baby";
var pattern = /mom( and dad ( and baby)?)?/gi;
var matches = pattern.exec(text);
alert(matches.index); //0
alert(matches.input); / /"mom and dad and baby"
alert(matches[0]); //"mom and dad and baby"
alert(matches[1]); //" and dad and baby"
alert(matches[2]); //" and baby"
Every time exec() is called in non-global mode, the first matched item is returned.
Every time in global mode Calling ecec() every time returns the next match of the string, and the value of lastIndex will be increased
The test() method accepts one parameter, which is the string to which the regular expression is applied. If it matches, it returns true, otherwise it returns false
var text = "000-00-0000";
var pattern = /d{3}-d{2}-d{4}/;
if (pattern.test(test)) {
alert("Match! ");
}
4.RegExp constructor attribute
var text = "this has been a short summer";
var pattern = /(.)host/g;
if (pattern.test(text) ){
alert(RegExp.input); //this has been a short summer The latest string to be matched, equivalent to RegExp.$_, opera does not support
alert(RegExp.lastMatch); / /short The most recent match, equivalent to RegExp["$&"], opera does not support
alert(RegExp.lastParen); //s The most recent matching capture group, equivalent to RegExp["$ " ] ,opera does not support
alert(RegExp.leftContext); //this has been a The text before the match, equivalent to RegExp["$`"]
alert(RegExp.rightContext); // summer The text after the matching item is equivalent to RegExp["$'"],
alert(RegExp.multiline); //false Is it multi-line mode, equivalent to RegExp["$*"], opera and IE None support
}
RegExp.$1 …… RegExp.$9 storage capture group
var text = "this has been a short summer";
var pattern =/(..)or(.)/g;
if ( pattern.test(text)) {
alert(RegExp.$1); //sh
alert(RegExp.$2); //t
}