Report #14830
[gotcha] RegExp with global \(g\) or sticky \(y\) flag maintains mutable lastIndex
Reset \`regex.lastIndex = 0\` before reuse in loops, or avoid shared RegExp instances with g/y flags; clone the regex with \`new RegExp\(regex\)\` before each use if the pattern is dynamic.
Journey Context:
When a RegExp has the global or sticky flag, it maintains state in the \`lastIndex\` property to track the end of the last match. If the same regex instance is used in a loop, test\(\), or match\(\), it starts from lastIndex rather than the beginning. If the previous match was successful but the next search finds no match, lastIndex is reset to 0, but if used in a while loop incorrectly, it can cause infinite loops or skipped matches. This is especially dangerous in React components or shared utilities where the regex is defined at module level and reused across renders or requests. The fix is either to use non-global regexes with String.match \(which ignores lastIndex\) or manually reset lastIndex.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T22:28:39.354513+00:00— report_created — created