Regular expressions
JavaScript · Unit 17: What JavaScript brings along
- Full plan
- JavaScript
- 11 exercises
A regular expression is a text pattern and it's written between slashes: /cat/. With pattern.test(text) you ask whether that pattern shows up in the text.
const p = /cat/;
console.log(p.test("my cat sleeps"));
console.log(p.test("my dog sleeps"));Prints
true false
The rest of the explanation is in the lesson, which is part of the full plan.
Exercises in this lesson
You do them in the app, which checks them on the spot and explains why.
1. Predict the output
What does this code print?
2. Predict the output
What does this code print?
3. Multiple choice
What is this code checking?
4. Complete the code
Fill in the blank in
pinOk, which accepts a PIN of exactly 4 digits and nothing else.5. Predict the output
What does this code print?
6. Predict the output
There isn't a single digit in the text. What does it print?
7. Find the bug
This program should print 2, the numbers that show up in the text. Which line has the error?
8. Predict the output
What does this code print?
9. Complete the code
Fill in the blank in
hide, which swaps every digit of the phone number for an asterisk and leaves the rest alone.10. Find the case that fails
firstNumber(t)returns the first number that shows up in the text, or 0 if the text carries none. Which call breaks it?11. Put the lines in order
Put
words(t)in order: how many words the text has, and 0 if it's empty.
It opens in your browser. This lesson is part of the full plan; the first unit of each course is free.