Skip to main content

JavaScript Regex Quantifiers — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Regex Quantifiers lesson. Every one is checked automatically in the browser — write the code, run it, and the tests tell you what still fails.

Exercises(5)

  1. 1

    Match one or more digits

    easy10 min

    Pull the whole number out of the text.

  2. 2

    Group runs of characters

    easy10 min

    Find every run of digits and join them.

  3. 3

    Make a letter optional

    easy10 min

    Match both spellings of the colour word.

  4. 4

    Require an exact count

    easy10 min

    Check the value is exactly four digits.

  5. 5

    Match lazily

    medium10 min

    Use a lazy quantifier to stop at the first closing bracket.

Find the bug(5)

  1. 1

    Fix the missing quantifier

    easy8 min

    Without a plus only one digit is matched.

  2. 2

    Fix the greedy match

    easy8 min

    A greedy quantifier swallows both tags instead of stopping at the first.

  3. 3

    Fix the star used where plus is needed

    easy8 min

    A star allows zero characters, so empty text passes.

  4. 4

    Fix the wrong exact count

    easy8 min

    A year needs four digits, not three.

  5. 5

    Fix the optional marker on the wrong character

    easy8 min

    The u is the optional letter, not the r.

Ready to write the code?

The editor, the automatic checks and the worked solutions open up with an account.

Open the workspace