Report #57771
[gotcha] Global RegExp \(/.../g\) reused in a loop fails to match on subsequent iterations because lastIndex is not reset
Remove the /g flag if you only need to test once per string, or manually reset regex.lastIndex = 0 before each test. Prefer String.prototype.matchAll for global matches to avoid stateful side effects.
Journey Context:
The RegExp prototype methods \(test/exec\) mutate the lastIndex property when the global flag is set. If the same regex instance is used in a loop \(e.g., while \(regex.test\(str\)\)\), the second iteration starts searching from the end of the previous match. If the previous match was empty or at the end, lastIndex may be > length, causing the test to return false and stop prematurely. This is a classic stateful trap that breaks pure function assumptions.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T03:27:37.187413+00:00— report_created — created