Skip to solution
mediumFrontend

What are the basics of regular expressions in JavaScript?

604 views
01

Understand the problem

Question presented to candidate: "If you create a regex with the global flag and call .test() on it three times in a row against the SAME short string, does it always return true? Walk me through what actually happens internally."

What a strong answer should cover:

  • 📌 Interview term: a regex literal (/pattern/flags) — matches text against a pattern; common methods are .test() (boolean), .exec() (a full match object or null), and the string methods .match()/.matchAll()/.replace().
  • 📌 Interview term: the real, direct answer to the prompt — verified directly: with the global (g) flag, .test()/.exec() are genuinely stateful — each call advances a real, persistent lastIndex property on the regex object itself, continuing the search from there on the NEXT call. Verified directly with 3 sequential real .test() calls against "xx": the answer is genuinely not always true — the third call genuinely returned false, because lastIndex had advanced past the end of the string, at which point it automatically resets to 0.
  • 📌 Interview term: capture groups — parentheses (...) create numbered capture groups, retrievable from a match — verified directly with a real date-parsing example; named capture groups ((?<name>...)) are also verified directly, retrievable via the match's own .groups object.
  • 📌 Interview term: match() with g vs. matchAll() — verified directly: str.match(/pattern/g) returns only the matched STRINGS, losing capture-group/index info, while matchAll() returns full match objects (with groups and index) for every match, genuinely richer information.
  • A precise answer names the real, common practical use of a capture-group-referencing .replace() ($1, $2, named-group syntax) for reformatting matched text — verified directly, reformatting a date string using its own captured pieces.

Clarifying questions expected:

  • None — this is a definitional/technical question; directly answering the prompt's own 3-calls scenario (and correctly explaining WHY the third call differs) is the strong signal, since global-flag statefulness is the single most commonly missed regex detail.

Code / implementation expected: Yes — the real, sequential 3-call .test() demonstration against the same short string is the clearest, most convincing proof of the exact statefulness behavior.

regexp
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every match, capture-group, and statefulness claim below was actually run in Node.

1.

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal proof: with the global flag, .test() is genuinely stateful across calls, resetting once lastIndex exceeds the string length
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 78 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track