Don’t overlook the usefulness of the negated character classes (
\D
,
\S
, and
\W
); they
represent a great way of getting rid of unwanted cruft. For example, it’s a great idea to
normalize phone numbers before storing in a database. People have all kinds of fussy
ways of entering phone numbers: dashes, periods, parentheses, and spaces. For
searching, keying, and identification, wouldn’t it be nice if they were just 10-digit
numbers? (Or longer if we’re talking about international phone numbers.) With
\D
,
it’s easy:
const
messyPhone
=
'(505) 555-1515'
;
const
neatPhone
=
messyPhone
.
replace
(
/\D/g
,
''
);
Similarly, I often use
\S
to make sure there’s data in required fields (they have to have
at least one character that’s not whitespace):
const
field
=
' something '
;
const
valid
=
/\S/
.
test
(
field
);
Repetition
Repetition metacharacters allow you to specify how many times something matches.
Consider our earlier example where we were matching single digits. What if, instead,
we wanted to match numbers (which may consist of multiple contiguous digits)? We
could use what we already know and do something like this:
const
match
=
beer99
.
match
(
/[0-9][0-9][0-9]|[0-9][0-9]|[0-9]/
);
Notice how we again have to match the most specific strings (three-digit numbers)
before we match less specific ones (two-digit numbers). This will work for one-, two-,
and three-digit numbers, but when we add four-digit numbers, we’d have to add to
our alternation. Fortunately, there is a better way:
const
match
=
beer99
.
match
(
/[0-9]+/
);
Note the
+
following the character group: this signals that the preceding element
should match one or more times. “Preceding element” often trips up beginners. The
repetition metacharacters are
modifiers that modify what comes before them. They do
not (and cannot) stand on their own. There are five repetition modifiers:
Repetition modifier
Description
Example
{n}
Exactly n.
/d{5}/
matches only five-digit numbers (such as
a zip code).
{n,}
At least n.
/\d{5,}/
matches only five-digit numbers or
longer.
{n, m}
At least n, at most m.
/\d{2,5}/
matches only numbers that are at
least two digits, but no more than five.
Repetition | 245