Quantifiers
A character class matches one position. A quantifier right after it says
how many times it may repeat: + means "one or more," {n,m} means "between
n and m times." \d+ matches a whole run of digits, however long - not just
one.
str.match(regex) (no g flag) returns an array whose first item, match[0],
is the matched text - or null if nothing matched. That's how you get the
actual text a pattern found, not just true/false.
Your task: write matchDigits(str), returning the first run of one or
more digits found in str (or null if there isn't one), and matchCode(str),
returning the first run of 2 to 4 digits found (or null).
You'll practice:
Matching a repeat with + (one or more)
Matching a count range with {n,m}
Extracting matched text with .match()
Show a hint
Show solution
Related reading: The Core Toolkit →
Previous Next