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 2

Characters, escapes and character classes interview questions

Interview questions on characters, escapes and character classes: warm-ups, then what each harder question tests and how a good answer starts.

12 questions: 4 warm-ups and 8 with model answers · Levels: Fresher to staff

Warm-up questions

Short questions with short answers, each answered in full.

  1. Fresher to mid

    How many characters are special outside a character class in JavaScript, and what makes any one of them literal?

    Answered in the lesson Literals, metacharacters and the dot

  2. Fresher to mid

    Why does one pattern need two backslashes in one place in your code and one in another?

    Answered in the lesson Writing patterns in code: strings and escaping

  3. Fresher to mid

    What does a negated class such as [^0-9] match, and how is it different from the dot?

    Answered in the lesson Character classes, ranges and negation

  4. Fresher to mid

    What do the three shorthand classes stand for, and why can one pattern match different characters in two languages?

    Answered in the lesson Shorthand classes for digits, words and spaces

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 log scanner uses /^log: .* done$/ to pick out single-line entries. Logs copied from a Windows file sometimes end with CRLF; copies from a web page sometimes carry a line separator (U+2028). Why does the pattern behave differently in Node.js and in Python here, and how do you write it so it behaves the same way everywhere?

    What it tests
    Knowing which code points each flavour keeps out of the dot, that only line feeds are kept out everywhere by default, and the portable idioms that cross lines.
    First step
    Name the four code points JavaScript keeps out of the dot (LF, CR, LS, PS) and the one Python, Go and PCRE2 keep out (LF alone). The scanner is single-line, so the pattern should reject every line terminator and the engines agree only on LF.
    Follow-ups

    From the lesson Literals, metacharacters and the dot

    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; a pass never renews. 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 4 minutes
  2. Mid to senior

    You port a pattern for the Devanagari letter स (U+0938), written as \u0938 in Python, to PCRE2 in a PHP service, to Go and to JavaScript. Walk through which spellings each engine accepts, the one that silently changes meaning in JavaScript, and the one escape all four share for code points below 256.

    What it tests
    Knowing JavaScript's \u, \u{...} and Annex B, Python's \u/\U/\N{name}, PCRE2's \x{...} and \N{U+...} and Go's \x{...}, and the one shared escape \xhh.
    First step
    Say which spellings each flavour accepts, then show which one changes meaning silently in JavaScript without the u flag; close with \xhh as the one two-hex form all four flavours have.
    Follow-ups

    From the lesson Literals, metacharacters and the dot

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  3. Mid to staff

    A bulk editor lets an analyst type the text to find and the new text. Analysts report that a dollar amount disappeared from the new text in the Go worker and that the Python worker stopped with a bad escape error. Some analysts also want group references. How do you build it?

    What it tests
    Knowing that replacement text has its own escapes per flavour, that a pattern escaper must not be used on it, and how to offer group references without breaking plain text.
    First step
    Separate the two sides in the design: the text to find is escaped with the engine own function, and the new text goes in through a function replacement, which no flavour reads for dollar or backslash forms. That one change explains both reports, because each worker read the new text as instructions.
    Follow-ups

    From the lesson Writing patterns in code: strings and escaping

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  4. Mid to senior

    A signup service in PHP checks usernames with preg_match('/^[[:alnum:]_-]+$/u', $name). Moved to Node.js as /^[[:alnum:]_-]+$/, it now rejects every username, with no error. Explain what JavaScript does with the pattern, and how you would port the check so both services accept the same names.

    What it tests
    Knowing that JavaScript has no POSIX names and reads the brackets as an ordinary class, spotting a silent failure, and porting a class by writing out the set the source flavour meant, Unicode included.
    First step
    Read the pattern as JavaScript does: the class ends at the first ], so [[:alnum:] is a set of seven characters, [ : a l n u m, and _-]+ is plain text after it. An anchored match then needs one of those seven, an underscore, a hyphen and one or more ], so AB-12 can never match.
    Follow-ups

    From the lesson Character classes, ranges and negation

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  5. Fresher to mid

    A registration form checks names with ^[A-Za-zÀ-ÿ]+$, meant as "letters, accents allowed". Testers report that A×B is accepted, and that Łukasz and नेहा are rejected. Explain all three results and propose a class that does what the form meant.

    What it tests
    Reading a range as an interval of code points, knowing what the Latin-1 block holds, and choosing Unicode properties, with the marks a script needs, over hand-made ranges.
    First step
    Look up the two ends of the range: À is U+00C0 and ÿ is U+00FF, so the class takes all 64 code points between them, and two of those are not letters: × (U+00D7) and ÷ (U+00F7). Then look up Ł and the Devanagari letters, which lie far past U+00FF.
    Follow-ups

    From the lesson Character classes, ranges and negation

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 3 minutes
  6. Mid to senior

    A Python service checks a six-digit delivery code with re.fullmatch(r"\d{6}", code) and stores the string. A Go service that prints labels later refuses some stored codes with strconv.Atoi: invalid syntax, though every one passed the check. What went wrong, and how do you fix the check and the codes already stored?

    What it tests
    Knowing that \d in a Python str pattern is any Unicode decimal digit while Go reads only 0 to 9, deciding whether to refuse or convert such input, and repairing data a check let through.
    First step
    Take one refused code and print the code point of each character. If they are Devanagari, Tamil or other Nd digits, the Python check accepted them because \d is Unicode for str patterns, and Go refused them because its parser and its \d know only 0 to 9. Then settle the field's real rule: ASCII digits only, or any script converted to ASCII before it is stored.
    Follow-ups

    From the lesson Shorthand classes for digits, words and spaces

    Model answer, Pro.

    In the answer:

    • Model answer
    • Follow-up answers
    • Common mistakes
    • Rubric
    • Variants
    • about 4 minutes
  7. Mid to senior

    A sign-up form checks the display name with /^\w+(?: \w+)*$/u in the browser. Names written in Devanagari or Tamil, and names such as Zoë or O'Neil, are refused, while a_b and Asha3 are accepted. Why did the u flag not help, and what check would you write instead, in the browser and on a Python server?

    What it tests
    Knowing that JavaScript's \w is ASCII in every mode, building a name class from letters, marks and joiners, allowing the separators names need, and keeping the browser and server rules the same.
    First step
    Run the pattern on each refused name and note the first code point it stops at: ë, every Devanagari and Tamil letter and mark, the apostrophe. The u flag changes how code points are read, not what \w holds, which stays 63 ASCII characters, so the class itself has to change.
    Follow-ups

    From the lesson Shorthand classes for digits, words and spaces

    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.