1. As long as the /g attribute is added to the regular expression, you need to pay attention to the change of the reg.lastIndex attribute value when executing the RegExp API
RegExp.exec(string)
Perform a search match in a specified string
exec(string): array || null
parameter
string
return value
Match successfully
- Returns an array, the 0th element of this array is the text that matches the regular expression, the first element is the text that matches the first sub-expression of RegExpObject (if any), and the second element is The text that matches the second sub-expression of RegExpObject (if any), and so on;
- In addition to the array elements and the length attribute, the exec() method also returns two attributes. The index attribute declares the position of the first character of the matched text. The input attribute stores the retrieved string string;
- RegExp does not have when global or sticky flag is set, reg.lastIndex is 0;
- When the global or sticky flag is set in RegExp, reg.lastIndex points to the next start position, which means that the exec method can be called repeatedly to traverse all matching text in the string; when exec() can no longer find a matching text , It will return null and reset the lastIndex property to 0
- Match failed
Return null
Precautions
- After the RegExp object has set the global or sticky flag, the RegExp object will have a state (the position after the last successful match will be recorded in the lastIndex property);
- After the RegExp object has set the global or sticky flag, if a new string is to be retrieved after a pattern match is completed in a string, the lastIndex property must be manually reset to 0.
demo
const regex1 = RegExp("foo*", "g");
const str1 = "table football, foosball";
let array1;
while ((array1 = regex1.exec(str1)) !== null) {
console.log(`Found ${array1[0]}. Next starts at ${regex1.lastIndex}.`);
// expected output: "Found foo. Next starts at 9."
// expected output: "Found foo. Next starts at 19."
}
refer to
RegExp.test(string)
Used to check whether the regular expression matches the specified string
test(string): bool
parameter
string
return value
bool
, match successfully true
; fail false
;
Precautions
- After the RegExp object has set the global or sticky flag, the RegExp object will have a state (the position after the last successful match will be recorded in the lastIndex property);
- After the RegExp object has set the global or sticky flag, if a new string is to be retrieved after a pattern match is completed in a string, the lastIndex property must be manually reset to 0.
- As long as there is a partial match, it will return true, you need to add
^
$
refer to
Add ^ $ before and after
RegExp.exec(string) and RegExp.test(string) matters needing attention
When using the RegExp object with the global flag set, you must pay attention to the change of the value of lastIndex; pay attention to modify the lastIndex as needed (for example, the same regular, after matching the A string, then matching the B string)
string.match(RegExp)
Retrieval returns the result of a string matching a regular expression
string.match(RegExp):array || null
parameter
RegExp
return value
Match successfully
- Array, RegExp with
g
global flag, returns all matches - Array, RegExp does not have the
g
global flag, and returns the first match
- Array, RegExp with
- Match incident
null
string.match(RegExp) and RegExp.exec(string)
If the regular expression does not contain the g flag, str.match() will return the same result as RegExp.exec().
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。