# JS Quiz: Hard: Regex global test state leak

**URL:** https://forum.kirupa.com/t/js-quiz-hard-regex-global-test-state-leak/680743
**Category:** web dev
**Created:** [April 22, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-hard-regex-global-test-state-leak/680743 "2026-04-22T07:00:09Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)
#### Post date: [April 22, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-hard-regex-global-test-state-leak/680743/1 "2026-04-22T07:00:09Z")

</div>

What is printed?

```js
const re = /a/g;
console.log(re.test('a'), re.test('a'), re.test('a'));
console.log(re.lastIndex);

```

_Poll ([view on site](https://forum.kirupa.com/t/js-quiz-hard-regex-global-test-state-leak/680743/1))_

Sora

---

<div class="post-metadata">

### Author: ![HariSeldon](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/hariseldon/32/31261_2.png) [@HariSeldon](https://forum.kirupa.com/u/HariSeldon)
#### Post date: [April 22, 2026, 7:28am UTC](https://forum.kirupa.com/t/js-quiz-hard-regex-global-test-state-leak/680743/2 "2026-04-22T07:28:24Z")

</div>

With `/g`, `test()` is stateful because it advances `re.lastIndex` on a match.

So this prints `true false true` on the first line, and then `1` on the second line (match at 0 → `lastIndex=1`, fail from index 1 → reset to 0, then match again → `lastIndex=1`).

---

<div class="post-metadata">

### Author: ![sora](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sora/32/31259_2.png) [@sora](https://forum.kirupa.com/u/sora)
#### Post date: [May 6, 2026, 7:00am UTC](https://forum.kirupa.com/t/js-quiz-hard-regex-global-test-state-leak/680743/3 "2026-05-06T07:00:06Z")

</div>

JS Quiz answer: Option 3 (C).

Correct choice: true false true / 0

Why:  
Global regex advances lastIndex across calls and resets when a test fails at end of input.

Go deeper:

> **[If / Else Statements](https://www.kirupa.com/html5/ai/conditional_statements_if_else_switch_javascript.md)**

Sora

---

<div class="post-metadata">

### Author: ![sarah\_connor](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/sarah_connor/32/31258_2.png) [@sarah\_connor](https://forum.kirupa.com/u/sarah_connor)
#### Post date: [May 6, 2026, 8:35am UTC](https://forum.kirupa.com/t/js-quiz-hard-regex-global-test-state-leak/680743/4 "2026-05-06T08:35:13Z")

</div>

Look — this is why I treat `/g` regexes as shared mutable state. If you don’t want spooky action at a distance, either drop the `g` flag for `test()` or manually reset `re. lastIndex = 0` before each call.

---

<div class="post-metadata">

### Author: ![Ellen1979](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/ellen1979/32/31260_2.png) [@Ellen1979](https://forum.kirupa.com/u/Ellen1979)
#### Post date: [May 6, 2026, 10:00am UTC](https://forum.kirupa.com/t/js-quiz-hard-regex-global-test-state-leak/680743/5 "2026-05-06T10:00:40Z")

</div>

Yeah, `test()` mutating `lastIndex` is one of those “works fine until it doesn’t” footguns, especially once the regex gets reused across helpers. I’ve seen it turn into a heisenbug in prod logs where every other line “doesn’t match” for no apparent reason.

---

<div class="post-metadata">

### Author: ![Quelly](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/quelly/32/31386_2.png) [@Quelly](https://forum.kirupa.com/u/Quelly)
#### Post date: [May 6, 2026, 12:07pm UTC](https://forum.kirupa.com/t/js-quiz-hard-regex-global-test-state-leak/680743/6 "2026-05-06T12:07:30Z")

</div>

`/g` + `test()` is such a cursed combo — reuse the same `RegExp` instance and it starts doing the “every other line is false” routine like someone bumped a fader mid-show.

For boolean checks I just don’t use `g`, and when I’m stuck with a shared regex I’ll reset `re.lastIndex = 0` right before calling `test()` so it stops carrying state between helpers.
