The RegExp object is an object in native JavaScript that represents a regular expression.
The object is created in the following way: var RegExp = new RegExp (pattern, attributes);
Parameter pattern specifies the regular expression rule or a string representing the regular expression pattern;
The parameter attributes is an optional parameter that represents the modifier for the matching Pattern. Consists of 3 parameters:
1. I: Perform an insensitive match to the case;
<script text= "text/javascript" > var txt = ' Hello world! ' var New RegExp (' Hello ', ' i '); if (reg.test (txt)) {console.log(txt.match (reg)); }</script>
2. G: Perform a global match (to find all occurrences rather than to find the first match and stop);
<script text= "text/javascript" > var txt = ' This is just a test. ' var reg = new RegExp (' is ', ' G '); // var reg01 = new RegExp (' is ', ' GI '); // case-insensitive if // ["is", "is"] console.log (txt.match (reg). length); // 2 } </script>
3. M: Performs multi-line matching (if This property is not set, ^ ($) only matches the start (end) position of the entire searched string; if this property is set, ^ ($) can also match the position of the searched string after "\ r" or "\ n").
<script type= "text/javascript" >//the following code is not able to match the string "an", although "an" is followed by a newline, but "an" is not the end of the string line. varTxt1 = ' This is an\n Apple '; varREG1 =/an$/; Console.log (txt1.match (reg1));//NULL //the following code can be matched to the string "an" varTxt2 = ' This is an\n Apple '; varREG2 =/an$/m; Console.log (txt2.match (reg2));//["an", index:8, input: "are you an?" Apple "] //the Following is a multi-line match beginning example varTXT3 = ' This is an\n Apple '; varReg3 =/^\sapp/; Console.log (txt3.match (reg3));//NULL varTXT4 = ' This is an\n Apple '; varREG4 =/^\sapp/m;///^ app/mConsole.log (txt4.match (reg4));//["app", index:11, input: "this was an?" Apple "]</script>
I,g,m three modifiers can be combined and used in combination with one another.
In the example above in the M modifier, var reg4 =/^\sapp/"\" is an escape character, and "\" in the regular expression should be replaced with "\ \" if the constructor is used to create the RegExp object:
<script type= "text/javascript" > var txt4 = ' This is an\n Apple '; var New RegExp (' ^\\sapp ', ' m '); Console.log (txt4.match (reg4)); // ["app", index:11, input: "this was an?" Apple "]</script>
RegExp Object Properties
1.global
Returns whether the regular expression has a "g" modifier;
<script type= "text/javascript" > var txt = ' This is just a test '; var New RegExp (' St ', ' G '); if (reg.global) { console.log (txt.match (reg)); // ["st", "st"] }</script>
2.ignoreCase
Returns whether the regular expression has an "i" modifier;
3.multiline
Returns whether the regular expression has an "m" modifier;
4.lastIndex
Marks the position of the string where the next match starts;
<script type= "text/javascript" > var txt = ' If you love code, You should code everyday. ' ; var New RegExp (' ou ', ' g '); var length = txt.match (reg). length; for (var i = 0; i < length; i++) { reg.test (txt); Console.log (reg.lastindex); } </script>
5.source
Returns a regular expression for pattern-matching text or expression that does not include the modifier "i", "g", "m" in the returned text, nor the delimiter used for the direct amount of the regular expression
<script> varnew RegExp (' Yoho ', ' m '); var New RegExp (' \\w '); var reg3 =/\w/m; Console.log (reg1.source); // Yoho Console.log (reg2.source); // \w Console.log (reg3.source); // \w</script>
RegExp Object Methods
1.compile
Change or recompile regular expressions (this method is not supported by opera Browser)
The following methods can be implemented for the redefinition of regular expressions, so I am not very clear about the application scenario for this method
<script> varnew RegExp (' ou ', ' g '); =/\w/; </script>
2.exec
Retrieving the matching results of a regular expression in a string
<script> var txt = ' If you love code, should code everyday. ' ; var New RegExp (' ou ', ' g '); var length = txt.match (reg). length; for (var i = 0; i < length; i++) { console.log (reg.exec (txt)); } </script>
3.test
Detects if a string matches a matching regular expression
<script> var txt = ' I code everyday. ' ; var New REGEXP (' code '); Console.log (reg.test (txt)); // true</script>
JavaScript RegExp Objects