Creating repeated patterns with quantifiers
Quantifiers modify the previous atom and request the particular number of repetitions. An atom is a character or character class or a string literal or a group (we will talk about groups later in the Extracting substrings with capturing section of this chapter).
The +
quantifier allows the previous atom to be repeated one or more times. For example, the regex /a+/
matches with a single character a
, as well as with a string containing two characters aa
, or three, or more—aaaaaa
. It will not, however, match with a string that does not contain the a
character at all.
The *
quantifier allows any number of repetitions, including zero. So, the /a*/
regex matches with strings such as bdef
, abc
, or baad
. Of course, a single /a*/
may not be that useful; the *
quantifier's more natural use case is between other substrings, such as /ab*c/
. This regex matches with either ac
, or abc
, or abbc
.
The ?
quantifier requires an atom to either appear once or to be absent...