Regular Expressions Module 7
Flags, modes and Unicode interview questions
Interview questions on flags, modes and Unicode: short warm-ups, then what each harder question tests, the first step of a good answer and its follow-ups.
Warm-up questions
Short questions with short answers, each answered in full.
-
Fresher to mid
What are three places a flag can go in a regex, and which engine refuses which?
A letter after the slash, as /a/i, in JavaScript, PCRE2, PHP, Ruby and Perl; a flags argument on the compile call, as re.compile(p, re.I) in Python or new RegExp(p, "i") in JavaScript; and an inline group inside the pattern, either global, (?i)pattern at the start of a Python, Go, PCRE2 or PHP pattern, or scoped, (?i:part) in every engine the lesson covers (in JavaScript since the ES2025 modifiers). JavaScript rejects the global (?i) as a SyntaxError, and Python 3.11 and later raise an error when such a group is not at the very start. Go has no flags argument at all, so Go patterns use the inline group. For a pattern built in code, a flags argument or a scoped group fits best; for a hand-written literal, the letter after the slash is the shortest.
From the lesson Flags and inline modifiers
-
Fresher to mid
Why do teams ask for long patterns to be split or commented, and what do you do in an engine that has no free-spacing mode?
Answered in the lesson Readable patterns: free-spacing and comments
Questions with model answers
What each question tests, the first step of a good answer and the follow-ups to expect.
-
Mid to senior
A JavaScript validator returns true, false, true for the same input:
const SIX = /\d{6}/g; export function isSixDigit(c) { return SIX.test(c); }. Explain what the engine is doing, fix it while keeping the compiled regex at module level, and name the test that would catch the bug.- What it tests
- Reading /g regex state, keeping a compiled regex while fixing the bug, choosing a fix over the shape of the function, and writing a test that catches the trap on a second call rather than only on the first.
- First step
- Reproduce the three answers in a quick repl: SIX.test('123456') three times in a row. The pattern of true, false, true tells you the g flag is the cause: the regex kept lastIndex, so the second call started past the end of the string. The fix depends on what the function must return and whether other code uses the same regex for iteration.
- Follow-ups
-
- What two safe fixes keep the shape of a boolean validator, and when would you reach for one over the other?
- Does adding anchors, as /^\d{6}$/g, remove the bug? Walk through the three calls with lastIndex at each step.
- If a caller later writes code.matchAll(SIX) with the fixed regex, what happens, and why does matchAll not have the trap you just fixed?
- What test case would you add so a change to the regex that adds the g flag back fails at CI?
From the lesson Flags and inline modifiers
Model answer, Pro. Model answers open with any pass; prices are in the notice on this page.Opening the model answers with your pass…
Model answers open with any MySmartCoPilot pass.
- Pro pass · 1 week₹79$2.99
- 30 days₹179$5.99
- 1 year₹1,699$45
One payment; nothing renews unless you choose to. The price shown is the total you pay: no fees or charges are added at checkout.
Passes can’t be bought right now, so the model answers can’t be opened at the moment. If you have a pass, enter your licence key.
It also opens every other in-depth part in Learn and every Pro tool. Sales are final, except where the law gives you a right to withdraw, for example in the EU and the UK (refund policy).
In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 5 minutes
-
Mid to senior
A Python 3.10 service finds lines that start with user: in any case, using
^(?i)user:.*$withre.M. The pattern must now run in Python 3.14, Go and a browser page. Give the exact pattern for each target, say where a scoped group fits better than a global(?i), and how you would prove the ports find the same lines.- What it tests
- Moving the global (?i) that Python 3.11 refuses, choosing scoped (?i:...) where it fits, writing a pattern with no flags argument in Go, and knowing that JavaScript refuses the global (?i) in every version.
- First step
- List the five ways a flag can go in a pattern and strike the ones each target refuses: Python 3.14 wants (?i) at the very start or (?i:...) around the word; Go has no flags argument; JavaScript rejects (?i) in every form other than scoped. Then pick the smallest change for each.
- Follow-ups
-
- Why does the (?i) in the middle of a Python 3.14 pattern raise an error, even though 3.10 took it?
- Does Go accept ^(?i)user:.*$ as it stands, and if so, what else do you check?
- What is the smallest JavaScript regex that matches the same lines as the Python 3.10 version?
- How would you keep one source file for the pattern, and generate the four forms from it?
From the lesson Flags and inline modifiers
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 5 minutes
-
Mid to senior
A support page builds a JavaScript pattern by joining
^, a prefix an agent types, a hyphen, a year and$; Go joins strings the same way. Prefixes such as A.B or C++ misbehave, and the page sometimes shows a 500 error. How do you build the pattern safely, and what does the test table hold?- What it tests
- Escaping plain text in JavaScript, Go and Python, wrapping parts before joining, and a test table with metacharacter rows, plus knowing when to avoid a pattern altogether.
- First step
- Print the composed pattern for A.B and for C++ and read it as the engine does: the dot matches any character and the plus signs are a quantifier on a quantifier, which fails to compile. Plain text typed by a person is data, not pattern.
- Follow-ups
-
- How does the escape differ in JavaScript, Go and Python?
- Why wrap the escaped prefix in a group before joining it to the rest?
- Which rows belong in the test table for this page?
- When is a regular expression the wrong tool for this check?
From the lesson Readable patterns: free-spacing and comments
Model answer, Pro.In the answer:
- Model answer
- Follow-up answers
- Common mistakes
- Rubric
- Variants
- about 4 minutes
All Regular Expressions interview questions · The Regular Expressions track