You will be able to use JavaScript regular expression quantifiers to specify how many times a character, group, or other token may repeat.
What it is
A quantifier is a regular expression operator that follows a token and controls repetition. A token can be a literal character, a character class such as [0-9], a dot, or a group such as (ab). In JavaScript, common quantifiers are ? for zero or one, * for zero or more, + for one or more, {n} for exactly n, {n,} for at least n, and {n,m} for between n and m times. Quantifiers are greedy by default, meaning they try to match as much as possible. Adding ? after a quantifier makes it lazy, so it tries to match as little as possible.
Why it matters
- It lets you validate bounded input, such as phone digits, ZIP codes, or password lengths.
- It reduces verbose patterns;
/a{3}/is clearer than/aaa/. - It helps match repeated structures, such as whitespace, digits, or optional prefixes.
- It gives precise control over how much text a pattern may consume.
- It supports both greedy and lazy matching when you need the longest or shortest possible match.
Syntax or steps
Place the quantifier immediately after the token you want to repeat. If you need to repeat more than one character, wrap those characters in a group.
token{min,max}
group{min,max}
token{min,max}?
For example, /a{2,4}/ repeats the letter a between 2 and 4 times, while /(ab){2,4}/ repeats the pair ab between 2 and 4 times.
Example
const bounded = /a{2,4}/;
const lazy = /a{2,4}?/;
const tests = ["a", "aa", "aaa", "aaaa", "aaaaa"];
for (const text of tests) {
console.log(text, bounded.test(text));
}
console.log("aaaa".match(lazy));
Part-by-part
/a{2,4}/matches any substring containing 2, 3, or 4 consecutiveacharacters.bounded.test("a")isfalsebecause there is only onea.bounded.test("aa"),bounded.test("aaa"), andbounded.test("aaaa")aretrue.bounded.test("aaaaa")is alsotruebecause the pattern can match a substring of fouracharacters./a{2,4}?/is lazy, so in"aaaa"it matches the shortest allowed run:"aa".
Common mistakes
- Repeating only the previous token:
/ab{2,4}/repeatsb, notab. Fix it with/(ab){2,4}/. - Forgetting that quantifiers match substrings unless anchored.
/a{2,4}/matches inside"baaab". Use/^a{2,4}$/if the whole string must be only 2 to 4acharacters. - Assuming greedy is always desired.
/a{2,4}/prefers fouracharacters when available. Use/a{2,4}?/when you want the shortest match. - Using an invalid range such as
{2,1}. The minimum must be less than or equal to the maximum.
When to use it
Use bounded quantifiers when the number of repetitions matters. Use simpler quantifiers when exact limits are unnecessary.
| Need | Use | Example |
|---|---|---|
| Optional token | ? | /colou?r/ |
| Zero or more | * | /a*/ |
| One or more | + | /a+/ |
| Exact range | {n,m} | /a{2,4}/ |
Practice
- Guided: Write a pattern that matches a ZIP code with 5 digits, optionally followed by a hyphen and 4 more digits. Use
/\d{5}(-\d{4})?/. Expected:"90210"matches,"90210-1234"matches, and"9021"does not. - Challenge: Write a pattern that matches a repeated word, such as
"the the". Hint: capture one word, require whitespace, then use a backreference:/\b(\w+)\s+\1\b/.
Quick check
Question: What does /a{2,4}/ match?
Answer: It matches a substring containing 2, 3, or 4 consecutive a characters. By default it is greedy, so it prefers the longest allowed run.
Summary
Quantifiers turn a single regex token into a repetition rule. In JavaScript, choose ?, *, +, or {n,m} based on whether the token is optional, unlimited, required, or bounded. Remember to group tokens when you need to repeat more than one character.