Report whether the text contains cat.
Acceptance criteria
- 1.#out should show found.
JavaScript Tutorial · JavaScript Regular Expressions
These 10 tasks go with the JavaScript Regular Expressions 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 regular expression describes a pattern of text rather than exact text.” .
1.Test for a wordexercise · easy · 10 min
Report whether the text contains cat.
Passes when: #out should show found.
2.Ignore the caseexercise · easy · 10 min · practice plan
Use a flag so a capital C still matches.
Passes when: #out should show found.
3.Find every matchexercise · easy · 10 min · practice plan
Count how many times cat appears.
Passes when: #out should show 2.
4.Replace every matchexercise · easy · 10 min · practice plan
Change all the cats to dogs.
Passes when: #out should show dog bat dog.
5.Build a pattern from a variableexercise · medium · 10 min · practice plan
Create the pattern with RegExp and test it.
Passes when: #out should show found.
6.Fix the pattern written in quotesbug fix · easy · 8 min
A pattern goes between slashes, not quotes.
Passes when: #out should show found.
7.Fix the missing i flagbug fix · easy · 8 min · practice plan
The capital letter stops the pattern matching.
Passes when: #out should show found.
8.Fix the missing g flagbug fix · easy · 8 min · practice plan
Without g only the first match is found.
Passes when: #out should show 2.
9.Fix only the first replacedbug fix · easy · 8 min · practice plan
Every cat should become a dog.
Passes when: #out should show dog bat dog.
10.Fix the unguarded matchbug fix · easy · 8 min · practice plan
A failed match gives null, which has no length.
Passes when: #out should show no matches.
Between them these tasks cover test, match and Flags.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.