Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

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.

6 questions: 2 warm-ups and 4 with model answers · Levels: Fresher to senior

Warm-up questions

Short questions with short answers, each answered in full.

  1. 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

  2. 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.

  1. 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

    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.

    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
  2. Mid to senior

    A Python 3.10 service finds lines that start with user: in any case, using ^(?i)user:.*$ with re.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

    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
  3. Mid to senior

    A Python service checks order lines with a long re.VERBOSE pattern, one commented field per line. A Node service needs the same rule. Pasting the text into new RegExp matches nothing; a script that strips spaces and comments then throws on one pattern and accepts bad input on another. Explain each failure and how to share one rule.

    What it tests
    Knowing that JavaScript has no free-spacing mode, what naive stripping does to escaped spaces and hashes, and how to keep one rule across engines with parts and a shared test table.
    First step
    Run the pasted text through new RegExp and read what the engine actually received: the spaces and the comment words are now required characters. Then diff the stripped text against the Python source, looking at every backslash-space and every hash inside a class or after a backslash.
    Follow-ups

    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
  4. 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

    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

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.