I'm getting some odd behavior in the chrome JavaScript console. How can this be possible?
start is 5
word is /[\w$]+/g
z is "z"
start && word.test(z)
=> false
(start && word.test(z))
=> true
start && (word.test(z))
=> false
I'm getting some odd behavior in the chrome JavaScript console. How can this be possible?
start is 5
word is /[\w$]+/g
z is "z"
start && word.test(z)
=> false
(start && word.test(z))
=> true
start && (word.test(z))
=> false
This has to do with the RegExp global flag. Thanks for @VLAZ for spotting this.
Specifically, the global flag is stateful. The trouble is that the code was run multiple times across the same session but the state of the RegExp was changing.
It looks like the idea with a global stateful RegExp is that it never matches the same section of the string twice.