Report #93452
[gotcha] RegExp with global flag mutates lastIndex causing subsequent test/exec to fail unexpectedly
Reset regex.lastIndex = 0 before each use, or avoid the g flag when testing multiple strings; use String.prototype.matchAll\(\) for iteration instead of exec\(\) in loops.
Journey Context:
When a RegExp has the global \(g\) or sticky \(y\) flag, the lastIndex property persists between calls to test\(\) or exec\(\). If you test a string that matches, lastIndex advances; the next test starts from that position, potentially missing matches or returning false unexpectedly. This causes flaky tests and infinite loops. The fix is to manually reset lastIndex to 0 before each execution, or better, avoid the g flag for one-off tests and use matchAll\(\) for iteration, which handles state correctly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T15:26:42.554477+00:00— report_created — created