Report #6543
[gotcha] RegExp with global \(/g\) or sticky \(/y\) flags produces incorrect match results when reused across strings
Reset regex.lastIndex = 0 before each use, or instantiate the RegExp inside the function/loop instead of reusing a module-level constant
Journey Context:
RegExp instances are stateful; exec\(\) and test\(\) update lastIndex. When reusing a /g regex on different strings, it starts from the previous match end, potentially skipping initial matches or returning null prematurely. This causes flaky tests that pass in isolation but fail in batch where the regex is reused. The fix is to treat regexes with /g as disposable or manually reset state, trading performance for correctness.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T00:19:22.860159+00:00— report_created — created