JavaScript provides a RegExp object to complete operations and functions related to regular expressions. Each regular expression pattern corresponds to a RegExp instance. There are two ways to create instances of RegExp objects.
Use RegExp's explicit constructor, syntax: new RegExp("pattern"[,"flags"]).
Use RegExp's implicit constructor, in plain text format: /pattern/[flags].
The pattern part is the regular expression pattern text to be used and is necessary. In the first way, the pattern part exists in the form of a JavaScript string and needs to be enclosed in double quotes or single quotes; in the second way, the pattern part is nested between two "/" and cannot be quotation marks.
The flags section sets the flag information of the regular expression, which is optional. If the flags part is set, in the first way, it exists in the form of a string; in the second way, it follows in the form of text immediately after the last "/" character. flags can be a combination of the following flag characters.
g is the global flag. If this flag is set, when searching and replacing operations are performed on a certain text, all matching parts of the text will work. If this flag is not set, only the earliest matching content is searched and replaced.
i is an ignorant case flag. If this flag is set, case will be ignored when matching and comparison is performed.
m is a multi-line sign. If this flag is not set, the metacharacter "^" only matches the start position of the entire searched string, while the metacharacter "$" only matches 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 (i.e. the beginning of the next line), and "$" can also match the position after "\n" or "\r" in the searched string (i.e. the end of the next line).
Code 8.1 is an example of creating regular expressions.
Code 8.1 Create regular expressions: 8.
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
Since "\" in a JavaScript string is an escape character, when creating a RegExp instance object using an explicit constructor, the "\" in the original regular expression should be replaced with "\\". For example, the two statements in code 8.2 are equivalent.
Code 8.2 "\" in escape characters: 8.
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
It can be seen that the results of both are the same.
Since the escaped characters in the regular expression pattern text are also "\", if the regex is to match the regex character "\", it should be represented by "\\" in the regular expression pattern text. When creating a RegExp instance object using an explicit constructor, you need to use "\\\\" to represent the regEx character "\".
var re = new RegExp(\\\\)。
8.4 Properties of RegExp Object
The properties of the RegExp object are divided into static properties and instance properties. The following are introduced separately.
8.4.1 Static properties
(1) Index attribute. It is the start position of the current expression pattern for the first time to match the content, counting from 0. Its initial value is -1, and the index attribute will change every time it matches successfully.
(2) Input attribute. Returns the currently used string, which can be abbreviated as $_, and the initial value is an empty string "".
(3) lastIndex attribute. It is the next position of the last character in the content for the first time the current expression pattern matches. It starts from 0 and is often used as the starting position when continuing to search. The initial value is -1, indicating that the search starts from the starting position. Each time the match is successful, the value of the lastIndex attribute will change accordingly.
(4) lastMatch attribute. It is the last matching string of the current expression pattern, which can be abbreviated as $&. Its initial value is an empty string "". Each time a successful match is matched, the lastMatch attribute value will change accordingly.
(5) lastParen attribute. If there is a enclosed submatch in the expression pattern, it is the substring matched by the last submatch in the current expression pattern, which can be abbreviated as $+. Its initial value is an empty string "". Each time a successful match is matched, the lastParen property value will change accordingly.
(6) leftContext property. It is the last matching everything on the left side of the string in the current expression pattern, and can be abbreviated as $` (where "'" is the anti-single quote below "Esc" on the keyboard). The initial value is an empty string "". Each time a successful match is matched, its attribute value will change accordingly.
(7) rightContext property. is the last matching everything to the right of the string in the current expression pattern, which can be abbreviated as $'. The initial value is an empty string "". Each time a successful match is matched, its attribute value will change accordingly.
(8) $1…$9 attributes. These properties are read-only. If there are enclosed submatches in the expression pattern, the $1…$9 attribute values are the content captured by the first to ninth submatches respectively. If there are more than 9 sub-matches, the $1…$9 attributes correspond to the last 9 sub-matches respectively. In an expression pattern, you can specify any number of parenthesed submatches, but the RegExp object can only store the results of the last 9 submatches. In the result array returned by some methods of the RegExp instance object, you can get the sub-match results in all parentheses.
8.4.2 Instance properties
(1) Global attributes. Returns the status of the global flag (g) specified when creating the RegExp object instance. If the g flag is set when creating a RegExp object instance, this property returns True, otherwise it returns False, and the default value is False.
(2) ignoreCase attribute. Returns the status of the ignoreCase flag (i) specified when creating the RegExp object instance. If the i flag is set when creating the RegExp object instance, this property returns True, otherwise it returns False, the default value is False.
(3) multiLine attribute. Returns the status of the multiLine flag (m) specified when creating the RegExp object instance. If the m flag is set when creating a RegExp object instance, this property returns True, otherwise it returns False, the default value is False.
(4) Source attribute. Returns the expression text string specified when creating the RegExp object instance.
8.5 Methods of RegExp Objects
Common methods of RegExp objects are test, exec and compile. This section introduces the functions and usage of these methods. Finally, a comprehensive example of the properties and methods of the RegExp object.
8.5.1 test method
The syntax format is test(str). This method checks whether there is an expression pattern specified when creating a RegExp object instance in a string, and returns True if it exists, otherwise returns False. If a match is found, the relevant static properties in the RegExp object are updated to reflect the match. Regarding the use of this method, it will be used frequently in the following section 8.10, and no separate examples will be given here.
8.5.2 exec method
The syntax format is exec(str). This method searches a string using the expression pattern specified when creating an instance of the RegExp object and returns an array containing the search results.
If a global flag (g) is set for a regular expression, you can search continuously in the string by calling the exec and test methods multiple times, each time starting 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 starting position of the string.
If the exec method does not find a match, the return value is null; if a match is found, an array is returned and the static properties in the RegExp object are updated to reflect the match. Element 0 in the return array contains the complete matching result, while elements 1 to n are the results of each sub-match defined in the expression pattern in sequence.
The array returned by the exec method has 3 properties, namely input, index and lastIndex.
The input property is the entire string being searched for.
The index attribute refers to the position that matches the entire searched string.
The lastIndex attribute refers to the next character position of the last character of the matching substring.
Code 8.3 is an example of the application of this method.
Code 8.3 The exec() method application: 8.
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
8.5.3 compile method
The syntax format is compile("pattern"[,"flags"]). This method can replace the expression pattern used by the RegExp object instance and compile the new expression pattern into an internal format, so that the subsequent matching process can be performed faster. If you want to reuse an expression in a loop, compiling it will speed up execution. However, if you use any other expression pattern in your program and then use the originally compiled expression pattern, this compilation is of no benefit.
Use RegExp's explicit constructor, syntax: new RegExp("pattern"[,"flags"]).
Use RegExp's implicit constructor, in plain text format: /pattern/[flags].
The pattern part is the regular expression pattern text to be used and is necessary. In the first way, the pattern part exists in the form of a JavaScript string and needs to be enclosed in double quotes or single quotes; in the second way, the pattern part is nested between two "/" and cannot be quotation marks.
The flags section sets the flag information of the regular expression, which is optional. If the flags part is set, in the first way, it exists in the form of a string; in the second way, it follows in the form of text immediately after the last "/" character. flags can be a combination of the following flag characters.
g is the global flag. If this flag is set, when searching and replacing operations are performed on a certain text, all matching parts of the text will work. If this flag is not set, only the earliest matching content is searched and replaced.
i is an ignorant case flag. If this flag is set, case will be ignored when matching and comparison is performed.
m is a multi-line sign. If this flag is not set, the metacharacter "^" only matches the start position of the entire searched string, while the metacharacter "$" only matches 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 (i.e. the beginning of the next line), and "$" can also match the position after "\n" or "\r" in the searched string (i.e. the end of the next line).
Code 8.1 is an example of creating regular expressions.
Code 8.1 Create regular expressions: 8.
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
Since "\" in a JavaScript string is an escape character, when creating a RegExp instance object using an explicit constructor, the "\" in the original regular expression should be replaced with "\\". For example, the two statements in code 8.2 are equivalent.
Code 8.2 "\" in escape characters: 8.
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
It can be seen that the results of both are the same.
Since the escaped characters in the regular expression pattern text are also "\", if the regex is to match the regex character "\", it should be represented by "\\" in the regular expression pattern text. When creating a RegExp instance object using an explicit constructor, you need to use "\\\\" to represent the regEx character "\".
var re = new RegExp(\\\\)。
8.4 Properties of RegExp Object
The properties of the RegExp object are divided into static properties and instance properties. The following are introduced separately.
8.4.1 Static properties
(1) Index attribute. It is the start position of the current expression pattern for the first time to match the content, counting from 0. Its initial value is -1, and the index attribute will change every time it matches successfully.
(2) Input attribute. Returns the currently used string, which can be abbreviated as $_, and the initial value is an empty string "".
(3) lastIndex attribute. It is the next position of the last character in the content for the first time the current expression pattern matches. It starts from 0 and is often used as the starting position when continuing to search. The initial value is -1, indicating that the search starts from the starting position. Each time the match is successful, the value of the lastIndex attribute will change accordingly.
(4) lastMatch attribute. It is the last matching string of the current expression pattern, which can be abbreviated as $&. Its initial value is an empty string "". Each time a successful match is matched, the lastMatch attribute value will change accordingly.
(5) lastParen attribute. If there is a enclosed submatch in the expression pattern, it is the substring matched by the last submatch in the current expression pattern, which can be abbreviated as $+. Its initial value is an empty string "". Each time a successful match is matched, the lastParen property value will change accordingly.
(6) leftContext property. It is the last matching everything on the left side of the string in the current expression pattern, and can be abbreviated as $` (where "'" is the anti-single quote below "Esc" on the keyboard). The initial value is an empty string "". Each time a successful match is matched, its attribute value will change accordingly.
(7) rightContext property. is the last matching everything to the right of the string in the current expression pattern, which can be abbreviated as $'. The initial value is an empty string "". Each time a successful match is matched, its attribute value will change accordingly.
(8) $1…$9 attributes. These properties are read-only. If there are enclosed submatches in the expression pattern, the $1…$9 attribute values are the content captured by the first to ninth submatches respectively. If there are more than 9 sub-matches, the $1…$9 attributes correspond to the last 9 sub-matches respectively. In an expression pattern, you can specify any number of parenthesed submatches, but the RegExp object can only store the results of the last 9 submatches. In the result array returned by some methods of the RegExp instance object, you can get the sub-match results in all parentheses.
8.4.2 Instance properties
(1) Global attributes. Returns the status of the global flag (g) specified when creating the RegExp object instance. If the g flag is set when creating a RegExp object instance, this property returns True, otherwise it returns False, and the default value is False.
(2) ignoreCase attribute. Returns the status of the ignoreCase flag (i) specified when creating the RegExp object instance. If the i flag is set when creating the RegExp object instance, this property returns True, otherwise it returns False, the default value is False.
(3) multiLine attribute. Returns the status of the multiLine flag (m) specified when creating the RegExp object instance. If the m flag is set when creating a RegExp object instance, this property returns True, otherwise it returns False, the default value is False.
(4) Source attribute. Returns the expression text string specified when creating the RegExp object instance.
8.5 Methods of RegExp Objects
Common methods of RegExp objects are test, exec and compile. This section introduces the functions and usage of these methods. Finally, a comprehensive example of the properties and methods of the RegExp object.
8.5.1 test method
The syntax format is test(str). This method checks whether there is an expression pattern specified when creating a RegExp object instance in a string, and returns True if it exists, otherwise returns False. If a match is found, the relevant static properties in the RegExp object are updated to reflect the match. Regarding the use of this method, it will be used frequently in the following section 8.10, and no separate examples will be given here.
8.5.2 exec method
The syntax format is exec(str). This method searches a string using the expression pattern specified when creating an instance of the RegExp object and returns an array containing the search results.
If a global flag (g) is set for a regular expression, you can search continuously in the string by calling the exec and test methods multiple times, each time starting 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 starting position of the string.
If the exec method does not find a match, the return value is null; if a match is found, an array is returned and the static properties in the RegExp object are updated to reflect the match. Element 0 in the return array contains the complete matching result, while elements 1 to n are the results of each sub-match defined in the expression pattern in sequence.
The array returned by the exec method has 3 properties, namely input, index and lastIndex.
The input property is the entire string being searched for.
The index attribute refers to the position that matches the entire searched string.
The lastIndex attribute refers to the next character position of the last character of the matching substring.
Code 8.3 is an example of the application of this method.
Code 8.3 The exec() method application: 8.
[Ctrl+A Select all Note:Introducing external Js requires refreshing the page before execution]
8.5.3 compile method
The syntax format is compile("pattern"[,"flags"]). This method can replace the expression pattern used by the RegExp object instance and compile the new expression pattern into an internal format, so that the subsequent matching process can be performed faster. If you want to reuse an expression in a loop, compiling it will speed up execution. However, if you use any other expression pattern in your program and then use the originally compiled expression pattern, this compilation is of no benefit.