Skip to main content

JavaScript Regular Expressions — exercises and bug fixes

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

    Test for a word

    easy10 min

    Report whether the text contains cat.

  2. 2

    Ignore the case

    easy10 min

    Use a flag so a capital C still matches.

  3. 3

    Find every match

    easy10 min

    Count how many times cat appears.

  4. 4

    Replace every match

    easy10 min

    Change all the cats to dogs.

  5. 5

    Build a pattern from a variable

    medium10 min

    Create the pattern with RegExp and test it.

Find the bug(5)

  1. 1

    Fix the pattern written in quotes

    easy8 min

    A pattern goes between slashes, not quotes.

  2. 2

    Fix the missing i flag

    easy8 min

    The capital letter stops the pattern matching.

  3. 3

    Fix the missing g flag

    easy8 min

    Without g only the first match is found.

  4. 4

    Fix only the first replaced

    easy8 min

    Every cat should become a dog.

  5. 5

    Fix the unguarded match

    easy8 min

    A failed match gives null, which has no length.

Ready to write the code?

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

Open the workspace