Skip to the solution
MediumFrontend6 min read

What are the basics of regular expressions in JavaScript?

604 views

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

Try it yourself

Sketch your approach before you read the solution. That is what the interview tests.

Stuck? Get a hint.

A short nudge toward the approach. It will not give the answer away.

Solution

The solution is hidden so you can try first

6 min read Β· 9 sections

Run the code

Edit the code and run it here. Each console line with an expected value becomes a test.

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

Discussion

Other approaches, follow-ups and how it went in real interviews.

Sign in to join the discussion.

No comments yet. Share how you would answer it.

Question 78 of 165 in JavaScript

Keep your momentum going.

Mark this question solved to track your progress, then take the next one while it is fresh.

Up nextMedium

How do you deep clone an object in JavaScript?

Continue