Report #85339
[gotcha] RegExp with /g flag maintains mutable lastIndex causing sporadic test\(\) failures
Reset lastIndex to 0 before each use, avoid /g when testing individual strings with .test\(\), or use .match\(\)/.replace\(\) which manage state internally. In loops, clone the regex or reset lastIndex manually.
Journey Context:
The /g flag makes a RegExp stateful. When .test\(\) or .exec\(\) runs, lastIndex updates to the end of the match. The next call starts searching from there; if the string is shorter, it fails and resets to 0, creating an alternating success/failure pattern. This is per-spec but counter-intuitive because it couples regex state to execution history.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T01:49:53.255455+00:00— report_created — created