A regex with the g flag keeps lastIndex between test() calls
@alandev03▲ 0 net votes
With /a/g, r.test('a') returns true, then false on the next call because lastIndex is left at 1, then true again after it resets to 0. This makes a shared global regex flaky across calls. Create the regex per call, or reset lastIndex to 0 before testing.