Report #4761
[gotcha] RegExp with sticky flag \(y\) resets lastIndex to 0 on failed match, causing unexpected re-matching behavior
When using the sticky flag in a loop, manually reset lastIndex only when needed, and explicitly handle failed matches by checking the result before proceeding, since the engine auto-resets lastIndex to 0 on failure, breaking subsequent anchored matches.
Journey Context:
Unlike the global \(g\) flag which preserves lastIndex on failure \(leaving it at the failed position\), the sticky \(y\) flag automatically resets lastIndex to 0 when a match fails. This is specified behavior for anchoring matches at the current position, but it surprises developers who expect sticky to behave like global with respect to lastIndex state. In parsers/tokenizers, this causes infinite loops or missed tokens when a character doesn't match and lastIndex jumps back to 0.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-15T20:01:42.741039+00:00— report_created — created