Javascript Regex with .test ()
The behavior is due to the modifier "g", i.e. corresponds to three times, does not correspond to the fourth time:
> var p = /a/gi; > var s = "aaa"; > p.test(s) true > p.test(s) true > p.test(s) true > p.test(s) false
See a similar question: Why does RegExp with a global flag in Javascript give incorrect results?
This is because of the / g flag. Each consecutive search starts with the last character matching the previous search. In your case, in the second run, it starts at the end of the line and returns false. The third time it starts from the beginning. Etc.
Also, take a look at this question: Why does RegExp with a global flag in Javascript give incorrect results?