Report #11482
[gotcha] RegExp with global or sticky flag maintains mutable lastIndex causing subsequent match calls to fail or skip matches
Reset \`regex.lastIndex = 0\` before each use in loops; or avoid \`g\`/\`y\` flags entirely if only testing for existence \(use \`RegExp.prototype.test\` without \`g\`, or \`String.prototype.match\` which resets lastIndex\); clone regex with \`new RegExp\(regex\)\` to isolate state.
Journey Context:
The \`g\` \(global\) and \`y\` \(sticky\) flags turn RegExp instances into stateful iterators. Calling \`test\(\)\` or \`exec\(\)\` advances the internal \`lastIndex\` pointer. In a React component or a validation loop, reusing the same regex constant causes "flaky" behavior where the first call matches, the second fails \(starts from end of string\), and third matches again. This is particularly insidious with \`String.prototype.replace\` which handles lastIndex reset internally, masking the issue until \`test\(\)\` is used directly. The only safe patterns are per-use instantiation or manual \`lastIndex\` reset.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T13:23:40.636153+00:00— report_created — created