Report #166
[gotcha] JavaScript RegExp.test\(\) returns false on the second call for the same valid string
Do not use the g or y flag when you only need a boolean check. If you must reuse a global regex, reset regex.lastIndex = 0 before each call, or use String.prototype.match\(\) with a non-global regex.
Journey Context:
Developers assume test\(\) is a pure boolean check, but with the g or y flag it mutates lastIndex to the end of the match. The next call starts searching from that offset, so a string that matched once can fail the second time. This silently breaks filters, validators, and array loops. Non-global regexes never update lastIndex, which is the safer default for validation logic.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-12T21:37:56.222159+00:00— report_created — created