Report #10516
[gotcha] RegExp sticky flag lastIndex coupling causes cross-input matching failures
Always manually reset \`regex.lastIndex = 0\` before using a sticky regex on a new string, or avoid reusing sticky regex instances across different input strings; prefer stateless regex creation or the \`g\` flag with careful anchoring
Journey Context:
Developers familiar with the \`g\` flag expect \`lastIndex\` to reset after a failed match, but ECMA-262 specifies that sticky mode \(\`y\`\) maintains \`lastIndex\` after failure to allow for incremental parsing. When the same regex instance is reused on a different, shorter string, the high \`lastIndex\` causes an immediate out-of-bounds failure. The alternative \(cloning regexes or manual index management\) adds overhead but prevents the state pollution.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T10:52:18.220003+00:00— report_created — created