The RegExp object in JS
JavaScript provides a RegExp object to complete the operation and functionality of regular expressions, and each regular expression pattern corresponds to a regexp instance. There are two ways to create an instance of a RegExp object.
Using the explicit constructor of REGEXP, the syntax is: New REGEXP ("pattern" [, "Flags"]).
Use the implicit constructor of RegExp, in plain text format:/pattern/[flags].
The pattern section is required for the regular expression schema text to be used. In the first way, the pattern part is in the form of a JavaScript string that needs to be enclosed in double quotes or single quotes; in the second way, the pattern portion is nested between two "/" and cannot be quoted.
The flags section sets the flag information for the regular expression and is optional. If the flags section is set, in the first way, in the form of a string, in the second way, the text is immediately after the last "/" character. Flags can be a combination of the following flag characters.
G is a global flag. If this flag is set, when a search and replace operation is performed on a text, all matching parts of the text will be in effect. If you do not set this flag, only the first matching content is searched and replaced.
I is ignoring the case flag. If this flag is set, the case is ignored when matching comparisons are made.
M is a multiline flag. If this flag is not set, then the meta character "^" matches only the beginning of the entire searched string, and the meta character "$" matches only the end position of the searched string. If this flag is set, "^" can also match the position after "n" or "r" in the searched string (that is, the beginning of the next line), "$" can also match the position after "n" or "r" in the searched string (that is, the line end of the next line).
Code 1.1 is an example of creating a regular expression.
Code 1.1 Creating a regular expression: 1.1.htm
<title> Create regular Expressions </title>
<script language = "JavaScript" >
var mystring= "This is an example of the first regular expression";
var Myregex = new RegExp ("one"); Creating regular Expressions
if (Myregex.test (myString)) {
document.write ("The specified pattern was found.) ");
}
else{
document.write ("The specified pattern was not found.) ");
}
</script>
The above code runs as shown in Figure 8.1.
Because "/" in a JavaScript string is an escape character, the RegExp instance object should be replaced with//in the original regular expression when it is created using an explicit constructor. For example, the two statements in code 1.2 are equivalent.
"/" In Code 1.2 escape characters: 1.2.htm
<script language= "JavaScript" >
var Re1 = new RegExp ("//d{5}");
var re2 =//d{5}/;
Alert ("re1=" +re1+ "/nre2=" +re2);
</script>
Because the escape character in the regular expression pattern literal is also "/", if you want to match the literal character "/" in the regular expression, in the regular expression pattern text, as "//", you need to use "////" to represent the literal character when you create the RegExp instance object by using an explicit constructor. /”。
var re = new RegExp (////). 1 Properties of the RegExp object
The properties of the RegExp object are divided into static and instance properties. The following are described separately. 1.1 Static Properties
(1) Index property. is the starting position of the first match for the current expression pattern, counting starting at 0. The initial value is-1, and the Index property will change each time a successful match is made.
(2) Input property. Returns the currently acting string, which can be abbreviated to $_, and the initial value is an empty string "".
(3) Lastindex property. Is the next position of the last character in the first match of the current expression pattern, counting from 0, often as the starting position for the search, with an initial value of-1, which indicates that the search starts at the starting position, and the Lastindex property value changes each time a successful match is made.
(4) Lastmatch property. is the last matching string for the current expression pattern, which can be abbreviated to $&. Its initial value is an empty string "". The value of the Lastmatch property changes each time a successful match is made.
(5) Lastparen property. If there is a $+ in the expression pattern, it is the substring that the last child match in the current expression pattern matches, and can be abbreviated to a string. Its initial value is an empty string "". Each time a successful match occurs, the value of the Lastparen property changes.
(6) Leftcontext property. Is all that is left of the last matching string in the current expression pattern, which can be abbreviated to $ ' (where "" is the inverted single quotation mark below "ESC" on the keyboard). The initial value is an empty string "". Each time a successful match is made, its property values change.
(7) Rightcontext property. is all content on the right side of the last matching string in the current expression pattern, which can be abbreviated to $ '. The initial value is an empty string "". Each time a successful match is made, its property values change.
(8) $1...$9 property. These properties are read-only. If there is a $1...$9 in the expression pattern, the value of the property is captured by the 1th to 9th child match. If there are more than 9 child matches, the $1...$9 property corresponds to the last 9 substring respectively. In an expression pattern, you can specify any number of child matches with parentheses, but the RegExp object can only store the result of the last 9 child matches. In an array of results returned by some methods of the RegExp instance object, the results of the child matches within all parentheses are obtained. 1.2 Instance Properties
(1) Global property. Returns the state of the global flag (g) specified when the RegExp object instance was created. If the G flag is set when the RegExp object instance is created, this property returns True, otherwise it returns false, and the default value is False.
(2) IgnoreCase property. Returns the state of the ignorecase flag (i) specified when the RegExp object instance was created. If the I flag is set when the RegExp object instance is created, this property returns True, otherwise it returns false, and the default value is False.
(3) Multiline property. Returns the state of the multiline flag (m) specified when the RegExp object instance was created. If the M flag is set when the RegExp object instance is created, this property returns True, otherwise it returns false, and the default value is False.
(4) The Source property. Returns the expression text string specified when the RegExp object instance was created.
The RegExp object in JS
2 Methods of RegExp objects
The common methods for RegExp objects are test, exec, and compile, and this section describes the functionality and usage of these methods. Finally, a comprehensive example of the properties and methods of the RegExp object is presented. 2.1 test Method
The syntax format is Test (str). This method checks whether a string has an expression pattern specified when the instance of the RegExp object is created, returns True if it exists, or returns false. If a match is found, the static properties in the RegExp object are updated to reflect the match.
2.2 Exec Method
The syntax format is exec (str). This method searches for a string using the expression pattern specified when the RegExp object instance is created, and returns an array that contains the results of the search.
If you set the global flag (g) for a regular expression, you can search the string continuously by calling the Exec and test methods multiple times, each time searching for a string from the location specified by the Lastindex property value of the RegExp object.
If the global flag (g) is not set, the Exec and test methods ignore the Lastindex property value of the RegExp object and start the search from the beginning of the string.
If the Exec method does not find a match, the return value is null, and if a match is found, an array is returned and the static property in the RegExp object is updated to reflect the match. The element 0 in the return array contains the full matching result, and the element 1~n, in turn, is the result of each child match defined in the expression pattern.
The Exec method returns an array that has 3 properties, namely input, index, and lastindex.
The input property is the entire string being searched.
The Index property refers to the position that matches the entire searched string.
The Lastindex property refers to the next character position of the last character of the matched substring.
Code 2.1 is an example of the application of this method.
Code 2.1 exec () method application: 2.1.htm
<title>exec () method application </title>
<script language = "JavaScript" >
var mystring= "AAA-BBB 222 CCC 1111 222DDD";
var regex =/111/; To create a regular expression object
var array=regex.exec (myString);
if (array) {
var str= "found a matching substring!" +/n Returns the array with the value: "+array+"/n array element number: "
+array.length+ "/n the searched string is:" +array.input
+ The start position of the "/N matching substring is:" +array.index
+/n matches the position of the first character after the substring: +regex.lastindex;
alert (str);
}
else{
Alert ("No matching substring found ... ");
}
</script>
<body></body>
2.3 Compile method
The syntax format is compile ("pattern" [, "Flags"]). The method can replace the expression pattern used by the RegExp object instance and compile the new expression pattern into an internal format so that subsequent matching procedures perform faster. If you want to reuse an expression in a loop, compiling it will accelerate the execution. However, if you use any other expression pattern in your program, and then use the original compiled expression pattern, this compilation is of no benefit. 2.4 Comprehensive Examples
Code 2.2 is a comprehensive example of the RegExp object, and careful analysis of the code and its running results can better understand the RegExp object.
Use of Code 2.2 regexp objects: 2.2.htm
<script language= "JavaScript" >
var strsrc = "Xxa1b01c001yya2b02c002zz";
var re =/A (d) b (/d{2}) C (/d{3})/gi;
var arr, count = 0;
while ((arr = re.exec (STRSRC))!= null)
{
Displayresult ();
}
function Displayresult ()
{
document.write ("<p> this is with regular expression/" + Re.source + "/gi to string <br>/" "
+ Regexp.input + "/" for the first "+ (++count) +" Results of the search:<br> ");
document.write ("Regexp.index" + Regexp.index + "<br>");
document.write ("Regexp.lastindex" + Regexp.lastindex + "<br>");
document.write ("Regexp.lastmatch" + Regexp.lastmatch + "<br>");
document.write ("Regexp.lastparen" + Regexp.lastparen + "<br>");
document.write ("Regexp.leftcontext" + Regexp.leftcontext + "<br>");
document.write ("Regexp.rightcontext" + Regexp.rightcontext + "<br>");
document.write ("regexp.$1" + regexp.$1 + "<br>");
document.write ("regexp.$2" + regexp.$2 + "<br>");
document.write ("regexp.$3" + regexp.$3 + "<br>");
document.write ("regexp.$4" + regexp.$4 + "<br>");
document.write ("Arr.index" + Arr.index + "<br>");
document.write ("Arr.input" + arr.input + "<br>");
document.write ("Arr.lastindex" + Arr.lastindex + "<br>");
document.write ("The number of elements to return an array is" + Arr.length + "<br>");
document.write ("Returns the contents of the array as [];")
for (var i=0; i<arr.length; i++)
{
if (I < arr.length-1)
document.write ("/" "+ arr[i] +"/",");
Else
document.write ("/" "+ arr[i] +"/") </p>");
}
}
</script>