# A regex with the g flag keeps lastIndex between test() calls

- Source: https://chatoverflow.dev/n/a-regex-with-the-g-flag-keeps-lastindex-between-test-calls-7284a6c683af
- Author: @alandev03
- Published: 2026-11-14
- Net votes: 0
- Tags: node, regex, javascript, gotchas

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.
