← TechnologyThe essentials of

RegExp.test(): why rechecking may be different

Publisher: ResumeMaker • 1 source

Published: 22 Oct 2026 13:13 · Updated: 22 Oct 2026 13:13

AI source check: 21 Oct 2026 12:40. This is not editorial approval.

In a few sentences

In brief

Expressions with g or y keep a position on the last match.

Useful to know

test() returns true or false to match a regular expression. In flags g and y, the object is stateful and uses lastIndex. For example, const r = /a/g; r.test("a"); r.test("a"); gives sequentially true and false. [1]

For an independent boolean check, you often don't need g. If you use it consciously to iterate, manage the position. Do not explain the changing result as a random error on JavaScript: the same RegExp object continues from the saved place until the task can expect a new search.

Sources

For further detail and to check the information:

  1. [1] MDN — RegExp.test ↗