Two ways to create a regular expression
Explicit:
NewRegExp("Pattern" [, "flags"]);
Example
var regex = new ("abc","gi");
The first parameter is the pattern to be matched, and the second parameter is the ID information, which has three types of ID information:
G: global IDEntifier, which indicates that it is used for all the matched parts of the text. If this parameter is not set, only search for and match the first text segment global
I: Ignore the case sensitive identifier ignoreCase
M: multi-line Identifier. If this identifier is not set, the metacharacter "^" and "$" only match the start position and end position of the text. If this identifier is set, it can also be matched with the position after "\ n" and "\ r" in the string, that is, the beginning and end of the next line
Implicit:
/Pattern/[flags] without quotation marks
Example
var regex = /\w+/i;var regex = new RegExp("\\w+","i");
The two are equivalent. When an explicit constructor is used, the special characters in the mode string should use escape characters, that is, the "\" leading symbol
Pattern match character
\ Indicates that the subsequent characters are special characters
^ Matched characters must be at the beginning
$ Matched characters must be at the end
* Match the first character 0 or n times
+ At least 1 time
? 0 or 1 time
. Match all individual characters except line breaks
X | y matches x or y
(X) mark the starting position and ending position of the subexpression
{N} matches the previous n characters
{N,} matches at least n characters
{N, m} matches the previous n to m characters
[Xyz] matches any listed characters
[^ Xyz] matches any character in the list, that is, matches any character other than the list character.
[A-d] match any character in the abcd
[\ B] matches a space.
\ B matches the dividing line of a word, such as a space
\ B matches the non-dividing line of a word
\ D matches a number, which is equivalent to [0-9].
\ D matches a non-digit
\ F matches a form character
\ N matches a linefeed.
\ R matches a carriage return.
\ S matches an empty single character, including space, Tab, feed, form, and line feed, which is equivalent to [\ f \ n \ r \ t \ v]
\ S matches a tab
\ W matches all letters, numbers, and underscores, equivalent to [A-Za-z1-9 _]
\ W matching \ w completion set
Greedy match and non-Greedy match
The regular expression uses the longest matching principle by default.
For example, match "bo?" in "book ?" And the result is "lok" instead of "look"
When "?" Followed by any other qualifier (*, + ,? , {N}, {n, m}), use non-Greedy Mode
For example, the string "booook" and "bo + ?" Only the "bo" part is matched, while "bo +" matches "boooo"
Select a match character
The two options of "|" are the largest expressions on both sides as much as possible.
For example, "abcd | efgh2" matches "abcd" or "efgh" instead of "abcd2" or "efgh2". To match "abcd2" or "efgh2 ", create "(abcd | efgh) 2"
Special characters
If you want to match the literal meanings of the following special characters, escape them "\"
^ $ * +? . () [] {} | \/
Static attributes of Regular Expressions
(1)IndexAttribute. It is the starting position of the First Matching content in the current expression mode and starts from 0. The initial value is-1. The index attribute changes every successful match.
(2)InputAttribute. Returns the current string, which can be abbreviated as $ _. The initial value is an empty string "".
(3)LastIndexAttribute. It is the next position of the last character in the first match of the current expression mode, which starts from 0 and is often used as the starting position for continued search. The initial value is-1, it indicates that the search starts from the starting position. The value of the lastIndex attribute changes each time the matching is successful.
(4)LastMatchAttribute. Is the last matching string of the current expression pattern, which can be abbreviated as $ &. Its initial value is an empty string "". The attribute value of lastMatch changes after each successful match.
(5)LastParenAttribute. If the enclosed child match exists in expression mode, it is the substring matched by the final child match in the current expression mode, which can be abbreviated as $ +. Its initial value is an empty string "". The value of the lastParen attribute changes each time a successful match is made.
(6)LeftContextAttribute. Is all content on the left of the last matching string in the current expression mode. It can be abbreviated as $ '("'" indicates the reverse quotation mark under "Esc" on the keyboard ). The initial value is an empty string "". The attribute value of each successful match changes accordingly.
(7)RightContextAttribute. Is all content on the right of the last matched string in the current expression mode, which can be abbreviated as $ '. The initial value is an empty string "". The attribute value of each successful match changes accordingly.
(8)$1... $9Attribute. These attributes are read-only. If the expression mode contains the enclosed child match, $1... The $9 attribute values are the content captured by 1st to 9th child matches. If more than 9 Child matches exist, $1... The $9 attribute corresponds to the last nine Child matches. In an expression mode, you can specify any number of child matches with parentheses, but the RegExp object can only store the last nine Child matches. In the result array returned by some methods of the RegExp instance object, you can obtain the child matching results in All parentheses.
Instance attributes
(1)GlobalAttribute. Returns the status of the global sign (g) specified when the RegExp object instance is created. If the g flag is set when the RegExp object instance is created, True is returned for this attribute. Otherwise, False is returned. The default value is False.
(2)IgnoreCaseAttribute. Returns the status of the ignoreCase flag (I) specified when the RegExp object instance is created. If the I flag is set when the RegExp object instance is created, True is returned for this attribute. Otherwise, False is returned. The default value is False.
(3)MultiLineAttribute. Returns the status of the multiLine flag (m) specified when the RegExp object instance is created. If the m flag is set when the RegExp object instance is created, True is returned for this attribute. Otherwise, False is returned. The default value is False.
(4)SourceAttribute. Returns the expression text string specified when the RegExp object instance is created.
RegExp object Method
TestMethod
The syntax format is test (str ). This method checks whether a string contains the expression mode specified when the RegExp object instance is created. If yes, True is returned; otherwise, False is returned. If a match is found, the static attributes in the RegExp object are updated to reflect the match.
ExecMethod
The syntax format is exec (str ). This method searches a string using the expression mode specified when the RegExp object instance is created, and returns an array containing the search results.
If a global sign (g) is set for the regular expression, you can perform a continuous search in the string by calling the exec and test Methods multiple times, each time, the string is searched from the location specified by the value of the lastIndex attribute of the RegExp object.
If the global flag is not set, the exec and test Methods ignore the lastIndex attribute value of the RegExp object and start searching from the start 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 attributes in the RegExp object are updated to reflect the match. Element 0 in the returned array contains the complete matching result, while element 1 ~ N is the result of each sub-match defined in expression mode in sequence.
The array returned by the exec method has three attributes: input, index, and lastIndex.
The input attribute is the entire searched string.
The index attribute refers to the position in the searched string.
The lastIndex attribute indicates the next character position of the last character of the matched substring.