Report #8372
[gotcha] RegExp with global flag mutates lastIndex causing skipped matches or infinite loops
Reset lastIndex to 0 before each search with regex.lastIndex = 0. Better: avoid the global flag for single matches; use String.prototype.matchAll for iteration \(returns a fresh iterator\), or clone the regex with new RegExp\(regex, regex.flags\) to reset state.
Journey Context:
The global flag makes a RegExp stateful. Each call to exec\(\) advances the lastIndex property. Reusing the same regex variable in a loop, conditional branch, or React render cycle causes it to start searching from the previous match end, often returning null unexpectedly or skipping characters. String.match\(\) with global returns an array without exposing this, but exec is needed for capture groups. Cloning the regex or explicitly resetting lastIndex is required for deterministic behavior.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T05:18:28.616670+00:00— report_created — created