Pull the whole number out of the text.
Acceptance criteria
- 1.#out should show 42.
JavaScript Tutorial · JavaScript Regular Expressions
These 10 tasks go with the JavaScript Regex Quantifiers 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: “Quantifiers say how many times a pattern should repeat.” .
1.Match one or more digitsexercise · easy · 10 min
Pull the whole number out of the text.
Passes when: #out should show 42.
2.Group runs of charactersexercise · easy · 10 min · practice plan
Find every run of digits and join them.
Passes when: #out should show 1,22,333.
3.Make a letter optionalexercise · easy · 10 min · practice plan
Match both spellings of the colour word.
Passes when: #out should show both spellings.
4.Require an exact countexercise · easy · 10 min · practice plan
Check the value is exactly four digits.
Passes when: #out should show a valid year.
5.Match lazilyexercise · medium · 10 min · practice plan
Use a lazy quantifier to stop at the first closing bracket.
Passes when: #out should show <a>.
6.Fix the missing quantifierbug fix · easy · 8 min
Without a plus only one digit is matched.
Passes when: #out should show 42.
7.Fix the greedy matchbug fix · easy · 8 min · practice plan
A greedy quantifier swallows both tags instead of stopping at the first.
Passes when: #out should show 3 characters.
8.Fix the star used where plus is neededbug fix · easy · 8 min · practice plan
A star allows zero characters, so empty text passes.
Passes when: #out should show rejected.
9.Fix the wrong exact countbug fix · easy · 8 min · practice plan
A year needs four digits, not three.
Passes when: #out should show a valid year.
10.Fix the optional marker on the wrong characterbug fix · easy · 8 min · practice plan
The u is the optional letter, not the r.
Passes when: #out should show both spellings.
Between them these tasks cover Quantifiers, Greedy matching and Lazy matching.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.