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 8.1 is an example of creating a regular expression.
Code 8.1 Creating a regular expression: 8.1.htm
<ptml> <pead> <title> Create regular expression </title> <script language = "JavaScript" > var mystring= "This is the first An example of a regular expression "; var Myregex = new RegExp ("one"); Create a regular expression if (Myregex.test (myString)) {document.write ("found the specified pattern!"). "); else{document.write ("The specified pattern was not found. "); } </script> </pead><body></body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
Because "\" in a JavaScript string is an escape character, when you use an explicit constructor to create a RegExp instance object, you should replace "\" in the original regular expression with "\". For example, the two statements in code 8.2 are equivalent.
"\" In code 8.2 escape characters: 8.2.htm
Copy Code code as follows:
<script language= "JavaScript" >
var Re1 = new RegExp ("\\d{5}");
var re2 =/\d{5}/;
Alert ("re1=" +re1+ "\nre2=" +re2);
</script>
As can be seen, both results are the same.
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 (\\\\).
8.4 Properties of RegExp objects
The properties of the RegExp object are divided into static and instance properties. The following are described separately.
8.4.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.
8.4.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.
methods of 8.5 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.
8.5.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. The use of this method is frequently used in the next 8.10 sections and is no longer a separate example.
8.5.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 8.3 is an example of the application of this method.
Code 8.3 exec () method application: 8.3.htm
<ptml> <pead> <title>exec () method application </title> <script language = "JavaScript" > var mystring= "a AA-BBB 222 CCC 1111 222DDD "; var regex =/111/; Create a regular Expression object var array=regex.exec (myString); if (array) {var str= "found a matching substring!" + \ n The value of the array is: "+array+" \ n the number of array elements: "+array.length+" \ n the string being searched is: "+array.input +" \ n matches the substring at the start position: "+array.index +" \ n the position of the first character following the substring is: "+REGEX.LASTINDEX; alert (str); else{alert ("No matching substring found!! "); } </script> <body></body> </ptml>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
8.5.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.