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
Collapse the whitespace
easy10 minTurn every run of whitespace into a single space.
- 2
Extract the numbers
easy10 minPull every number out and total them.
- 3
Build a slug
easy10 minTurn the title into a URL slug.
- 4
Trim the stray dashes
easy10 minRemove a dash from the start and end of the slug.
- 5
Escape user input
medium10 minEscape the dot so it matches literally.
Find the bug(5)
- 1
Fix the single space replacement
easy8 minReplacing one space misses tabs and longer runs.
- 2
Fix the numbers left as text
easy8 minMatches come back as strings, so they join rather than add.
- 3
Fix the slug with a trailing dash
easy8 minPunctuation at the end of the title leaves a dash behind.
- 4
Fix the unescaped user input
easy8 minAn unescaped dot matches any character.
- 5
Fix the regex used for exact text
easy8 minA 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