Report #74449
[gotcha] RegExp with global flag returns incorrect boolean on second test\(\) call
Reset \`regex.lastIndex = 0\` before each test/exec, or avoid the /g flag when reusing a regex for testing.
Journey Context:
When a RegExp has the global \(g\) or sticky \(y\) flag, the \`lastIndex\` property is updated to the character position after the last match. Subsequent calls to \`test\(\)\` or \`exec\(\)\` start searching from \`lastIndex\`, causing them to miss matches that appear earlier in the string or return \`false\` when they should return \`true\`. This is specified in ECMA-262 and is a frequent source of bugs when developers reuse a regex object in a loop or conditional check. Removing the /g flag prevents lastIndex mutation, or manually resetting it ensures consistent behavior.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T07:33:42.177012+00:00— report_created — created