Because of the flag g . It starts to remember the last index of the match, and the next time you do r.test , it starts with that index. Therefore, it alternates between true and false . try it
var r = /e/gi; console.log(r.test('e')); # true console.log(r.lastIndex); # 1 console.log(r.test('e')); # false console.log(r.lastIndex); # 0 console.log(r.test('e')); # true console.log(r.lastIndex); # 1 console.log(r.test('e')); # false
Quoting the MDN documentation on RegExp.lastIndex ,
lastIndex is a property of integer read / write regular expressions that indicates the index from which to start the next match ....
This property is set only if the regular expression uses the "g" flag to indicate global search. The following rules apply:
- If
lastIndex greater than the length of the string, test() and exec() do not work, then lastIndex set to 0. - If
lastIndex is equal to the length of the string, and if the regular expression matches the empty string, then the regular expression matches the input starting with lastIndex . - If
lastIndex is equal to the length of the string, and if the regular expression does not match the empty string, then the regular expression does not match the value entered, and lastIndex is reset to 0. - Otherwise,
lastIndex set to the next position after the last match.
The bold text above is responsible for the behavior you observed. After the first e match, the lastIndex parameter is set to 1 to indicate the index from which to try the next match. According to the 3rd paragraph above, since lastIndex is equal to the length of the string, and the regular expression does not match the empty string, it returns false and resets lastIndex to 0.
source share