Report #11273
[gotcha] RegExp test\(\) or exec\(\) intermittently returns false after previous global match
Reset \`regex.lastIndex = 0\` before each execution, or remove the \`/g\` flag when testing single strings. Prefer \`String.prototype.matchAll\(\)\` for iterating multiple matches to avoid stateful pollution.
Journey Context:
The \`/g\` flag causes RegExp instances to be stateful. After a match, \`lastIndex\` is updated to the character following the match. Subsequent tests on different strings begin searching from this stale index, causing misses. Developers often assume regexes are pure functions. Removing \`/g\` is safe for one-off tests. \`matchAll\` returns an iterator and does not mutate \`lastIndex\`, providing a safer abstraction for loops.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T12:53:18.330015+00:00— report_created — created