Skip to main content

JavaScript Regex in Practice — exercises and bug fixes

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

    Collapse the whitespace

    easy10 min

    Turn every run of whitespace into a single space.

  2. 2

    Extract the numbers

    easy10 min

    Pull every number out and total them.

  3. 3

    Build a slug

    easy10 min

    Turn the title into a URL slug.

  4. 4

    Trim the stray dashes

    easy10 min

    Remove a dash from the start and end of the slug.

  5. 5

    Escape user input

    medium10 min

    Escape the dot so it matches literally.

Find the bug(5)

  1. 1

    Fix the single space replacement

    easy8 min

    Replacing one space misses tabs and longer runs.

  2. 2

    Fix the numbers left as text

    easy8 min

    Matches come back as strings, so they join rather than add.

  3. 3

    Fix the slug with a trailing dash

    easy8 min

    Punctuation at the end of the title leaves a dash behind.

  4. 4

    Fix the unescaped user input

    easy8 min

    An unescaped dot matches any character.

  5. 5

    Fix the regex used for exact text

    easy8 min

    A plain string method is clearer for a fixed ending.

Ready to write the code?

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

Open the workspace