Skip to main content

JavaScript Regex Methods — exercises and bug fixes

5 hands-on exercises and 5 find-the-bug challenges for the JavaScript Regex Methods 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

    Find a position

    easy10 min

    Report where the first digit appears.

  2. 2

    Count every match

    easy10 min

    Count how many codes the text holds.

  3. 3

    Keep the groups with matchAll

    easy10 min

    Read a group from the second match.

  4. 4

    Replace with a function

    easy10 min

    Double every digit in the text.

  5. 5

    Split on a pattern

    medium10 min

    Split the text wherever there are digits.

Find the bug(5)

  1. 1

    Fix match used where a position is wanted

    easy8 min

    match gives the text, not the index.

  2. 2

    Fix the groups lost by the g flag

    easy8 min

    match with g returns strings only, so use matchAll.

  3. 3

    Fix matchAll without the g flag

    easy8 min

    matchAll requires the global flag.

  4. 4

    Fix the replacement returning nothing

    easy8 min

    The function must return the replacement text.

  5. 5

    Fix the stateful g pattern

    easy8 min

    A g pattern remembers its position between test calls.

Ready to write the code?

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

Open the workspace