Apparently regular expressions with the g
modifier are stateful. See for yourself.
<button id='bad'>Wait, WHAT?</button>
stateful = /button/ig;
bad.onclick = function (e) {
alert(stateful.test(e.target.tagName));
};
The button alternates between alerting true
and false
in Google Chrome 38.0.2125.122
, and also on Firefox 31
.
As it turns out, this is the “expected” behavior. I did not expect that. Regular expressions with the g
modifier on them will keep track of the lastIndex
where a match occurred, even across calls to .test
.
You can check out the live example on CodePen, too.
Comments