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
Find a position
easy10 minReport where the first digit appears.
- 2
Count every match
easy10 minCount how many codes the text holds.
- 3
Keep the groups with matchAll
easy10 minRead a group from the second match.
- 4
Replace with a function
easy10 minDouble every digit in the text.
- 5
Split on a pattern
medium10 minSplit the text wherever there are digits.
Find the bug(5)
- 1
Fix match used where a position is wanted
easy8 minmatch gives the text, not the index.
- 2
Fix the groups lost by the g flag
easy8 minmatch with g returns strings only, so use matchAll.
- 3
Fix matchAll without the g flag
easy8 minmatchAll requires the global flag.
- 4
Fix the replacement returning nothing
easy8 minThe function must return the replacement text.
- 5
Fix the stateful g pattern
easy8 minA 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