Lesson 44 of 48
92%
Regular Expressions
A regular expression is a pattern for matching text.
You describe the shape you want. Then you search for it.
Think of a stencil. The stencil shows the outline you accept.
1
Make a pattern
2
test and match
3
Simple special marks
4
replace with a pattern
5
Flags
Write a regex
Put the pattern between slashes like /cat/.
You can also use new RegExp.
javascript
test
test asks yes or no. Does the text contain a match?
javascript
match
match returns the found pieces, or null.
javascript
Simple special marks
\\d means a digit. . means any character.
^ means start. $ means end.
javascript
replace
You can replace matches with new text.
javascript
Flags
i ignores case. g finds all matches.
javascript
Tip: Start with plain text patterns. Add special marks only when a simple includes check is not enough.
Test yourself
Three quick questions made just for this lesson. Earn 10 XP per correct answer.