Turn every run of whitespace into a single space.
Acceptance criteria
- 1.#out should show a b c.
JavaScript Tutorial · JavaScript Regular Expressions
These 10 tasks go with the JavaScript Regex in Practice chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “A few everyday patterns cover most of what regular expressions are used for.” .
1.Collapse the whitespaceexercise · easy · 10 min
Turn every run of whitespace into a single space.
Passes when: #out should show a b c.
2.Extract the numbersexercise · easy · 10 min · practice plan
Pull every number out and total them.
Passes when: #out should show 15.
3.Build a slugexercise · easy · 10 min · practice plan
Turn the title into a URL slug.
Passes when: #out should show hello-world-2026.
4.Trim the stray dashesexercise · easy · 10 min · practice plan
Remove a dash from the start and end of the slug.
Passes when: #out should show middle.
5.Escape user inputexercise · medium · 10 min · practice plan
Escape the dot so it matches literally.
Passes when: #out should show exact match only.
6.Fix the single space replacementbug fix · easy · 8 min
Replacing one space misses tabs and longer runs.
Passes when: #out should show a b c.
7.Fix the numbers left as textbug fix · easy · 8 min · practice plan
Matches come back as strings, so they join rather than add.
Passes when: #out should show 15.
8.Fix the slug with a trailing dashbug fix · easy · 8 min · practice plan
Punctuation at the end of the title leaves a dash behind.
Passes when: #out should show clean slug.
9.Fix the unescaped user inputbug fix · easy · 8 min · practice plan
An unescaped dot matches any character.
Passes when: #out should show exact match only.
10.Fix the regex used for exact textbug fix · easy · 8 min · practice plan
A plain string method is clearer for a fixed ending.
Passes when: #out should show a pdf.
Between them these tasks cover Whitespace, Slugs and Escaping input.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.