Pay attention to the lastIndex attribute for regular expressions in JS,
Description
This article focuses on the attributes of the RegExp object in JavaScript.
Explanation
Each RegExp object contains five attributes: source, global, ignoreCase, multiline, and lastIndex.
Source: a read-only string that contains the text of a regular expression.
Var reg =/JavaScript/; reg. source; // return JavaScript
Global: a read-only Boolean value. Check whether the regular expression carries a modifier g.
The modifier g indicates a global match. It retrieves all the matches in the string.
Var str = "JavaScript"; str. match (/JavaScript/); // only one JavaScript var str = "JavaScript"; str. match (/JavaScript/g); // match two JavaScript var reg =/JavaScript/; reg. global; // return falsevar reg =/JavaScript/g; reg. global; // return true
IgnoreCase: a read-only Boolean value to check whether the regular expression carries the modifier I.
Modifier I, indicating that pattern matching is case insensitive.
Var reg =/JavaScript/; reg. ignoreCase; // return falsevar reg =/JavaScript/I; reg. ignoreCase; // returns truevar reg =/JavaScript/; reg. test ("javascript"); // returns falsevar reg =/JavaScript/I; reg. test ("javascript"); // return true
Multiline: a read-only Boolean value. Check whether the regular expression carries the modifier m.
Modifier m, used to perform matching in multi-line mode, must be used with ^ and $ </code>, use <code >^</code> and <code >$ to match the start and end of each line in addition to the start and end of the entire string.
Var str = "java \ nJavaScript"; str. match (/^ JavaScript/); // return nullvar str = "java \ nJavaScript"; str. match (/^ JavaScript/m); // match to a JavaScriptvar reg =/JavaScript/; reg. multiline; // return falsevar reg =/JavaScript/m; reg. multiline; // return true
LastIndex: a readable/written integer. If the matching mode contains a g modifier, this attribute is stored at the start position of the next retrieval of the entire string, and this attribute is exec () and the test () method.
The exec () method performs a match search in a string. If no match is found, it returns null, but if it finds a match, it returns an array.
When the regular expression object that calls exec () has a modifier g, it sets the lastIndex attribute of the current regular expression object to the character location next to the matching substring, when the same regular expression calls exec () for the second time, it will start to search from the string indicated by the lastIndex attribute. If exec () does not find any matching results, it resets the lastIndex to 0.
The test () method. Its parameter is a string. test () is used to detect a string. if it contains a matching result of a regular expression, true is returned. Otherwise, false is returned.
Var str = "java"; var reg =/JavaScript/; reg. test (str); // return falsevar str = "JavaScript"; var reg =/JavaScript/; reg. test (str); // return true
When the regular expression object that calls test () has the modifier g, it performs the same behavior as exec () because it retrieves a string from the position specified by lastIndex, if it finds a matching result, it immediately sets lastIndex to the character location next to the matching substring.
Let's take a look at the following interesting code.
Var str = "JavaScript"; var reg =/JavaScript/g; console. log (reg. test (str); // print trueconsole. log (reg. test (str); // print false
Why are the same strings and regular expressions printed differently? If you have understood the lastIndex attribute, you must understand why.
Let's take a look at what happened.
Var str = "JavaScript"; var reg =/JavaScript/g; console. log (reg. test (str); // print trueconsole. log (reg. lastIndex); // print 10. Because it matches JavaScript, set lastIndex to the character location next to the matching result console. log (reg. test (str); // print false, because the string is retrieved from the lastIndex position, no matching result is found on the console. log (reg. lastIndex); // print 0. Because no result is matched, The lastIndex is reset to 0.
Note that if we manually reset lastIndex to 0 after the first call of test () matches, the second call of test () can also print true
Var str = "java JavaScript java"; var reg =/JavaScript/g; console. log (reg. test (str); // print truereg. lastIndex = 0; console. log (reg. test (str); // print true
The lastIndex mentioned above is because the regular expression object has a modifier g. If there is no modifier g, you don't have to worry about these issues.
Summary
This article mainly talks about the five attributes of the regular expression object in JavaScript, and the most important thing to note is the lastIndex attribute.