Basics
Regular expressions are typically formatted as /<rules>/<flags>
. Often people will drop the slashes and flags for brevity. We’ll get into the details of flags in a later chapter.
Let’s start with the regex /p/g
. For now, please take the g
flag for granted.
/p/g
[RegExr] [Visual]- 1 match
pancake
p
- 3 matches
pineapple
p
p
p
- 2 matches
apple
p
p
- 0 matches
mango
- 0 matches
Plum
As we can see, /p/g
matches all lowercase p
characters.
Note
Regular expressions are case-sensitive by default.
Instances of the regex pattern found in an input string are termed “matches”.
/pp/g
[RegExr] [Visual]- 1 match
apple
pp
- 1 match
pineapple
pp
- 1 match
happiness
pp
- 2 matches
sipping apple juice
pp
pp
- 0 matches
papaya