When using regular expressions in JavaScript, a pit is encountered: The first match is true, and the second match is false.
Because there is a "LastIndex" property in a regular expression object with the global identity "G", this property is used to specify the starting position of the next match.
For example:
var New // with global identity var New // with no global identity
With the global identity of the regular pattern1, after the string is matched to the substring, the value of Pattern1.lastindex is not immediately reset to 0, but the last matching position continues to look backwards to see if there is a matching value, which can cause the problem: " A matching string matches the first time to true, and the second match is false.
There are two ways to resolve this:
(1) The Global Identity "G" is removed, and a regular expression is defined with a string wrapped in [^][$] end-to-end qualifier;
(2) In some cases, the global identity "G" must be preserved, you can manually reset the value of lastindex to 0 after a match (test), such as:
Pattern1.lastindex = 0
Lastindex property of the global identity regular expression in [JavaScript] js