← Back to JavaScript Course | Chapter 9: Async & Web APIs | Lesson 24 of 26

JS RegExp Quantifiers

Quantifiers control how many times the preceding part of a pattern must repeat to count as a match -- * for zero or more, + for one or more, ? for zero or one, and {n,m} for a specific range -- turning a single-character pattern into one that matches variable-length runs of that same thing.

Zero or More: *

* matches zero or more occurrences of the preceding element -- ab*c matches "ac" (zero b's), "abc" (one b), "abbbc" (many b's), and so on, since even zero repetitions still counts as a valid match.

Note: Remember that * allows for zero occurrences -- if at least one is actually required, use + instead.

Warning: A pattern relying on * where + was intended can match an unexpectedly short (or even empty) string, since zero repetitions still satisfies *.

Example: Zero or More: *

javascript
console.log(/ab*c/.test("ac"));    // true, zero b's
console.log(/ab*c/.test("abbbc")); // true, many b's

One or More: +

+ requires at least one occurrence of the preceding element -- \d+ matches "5", "42", or "123456", but fails to match anywhere at all in a string with no digits, unlike * which would still technically match zero characters.

Note: Use + instead of * whenever the preceding element genuinely must appear at least once for the match to be meaningful.

Warning: Confusing + and * is one of the most common regex mistakes -- always double-check whether "zero" is actually a valid count for what you are matching.

Example: One or More: +

javascript
console.log(/\d+/.test("42"));  // true
console.log(/\d+/.test("abc")); // false, no digits at all

Zero or One: ?

? makes the preceding element optional -- present zero or one time, never more -- colou?r matches both "color" (without the u) and "colour" (with it), handling both American and British spellings with a single pattern.

Note: Use ? to handle a genuinely optional single character or short sequence, like an optional letter or an optional trailing character.

Warning: ? only allows zero OR one occurrence -- it does not mean "optional and repeatable"; for that, combine it with a group and a different quantifier.

Example: Zero or One: ?

javascript
console.log(/colou?r/.test("color"));   // true
console.log(/colou?r/.test("colour"));  // true

Specific Ranges with {n,m}

{n} matches exactly n occurrences, {n,} matches at least n, and {n,m} matches between n and m occurrences (inclusive) -- giving precise control over repetition count beyond the broader */+/? shorthand, useful for things like validating a fixed-length code.

Note: Use {n,m} whenever a repetition count has specific, known bounds, like a 5-digit zip code or a password requiring 8 to 20 characters.

Warning: Writing {n} when {n,} or {n,m} was intended (or vice versa) is an easy typo that silently changes the matching rules -- always double-check which bound(s) you actually want.

Example: Specific Ranges with {n,m}

javascript
console.log(/^\d{3}$/.test("123"));   // true, exactly 3
console.log(/^\d{2,4}$/.test("12345")); // false, too many

Greedy vs Lazy Matching

By default, quantifiers are greedy -- they match as much text as possible while still allowing the overall pattern to succeed. Adding a ? right after a quantifier (like *? or +?) makes it lazy instead, matching as little text as possible, which matters when a pattern could otherwise "overshoot" across more text than intended.

Note: Use a lazy quantifier when matching content between two delimiters (like quotes or HTML tags) that might appear multiple times in the same string, to avoid matching from the first delimiter all the way to the last one.

Warning: A greedy quantifier used to match "between two quotes" in a string containing multiple quoted sections will match from the very first quote to the very last one, not the nearest matching pair.

Example: Greedy vs Lazy Matching

javascript
console.log("<a><b>".match(/<.*>/)[0]);  // greedy: "<a><b>"
console.log("<a><b>".match(/<.*?>/)[0]); // lazy: "<a>"
Common Mistakes
  1. Using * (zero or more) when + (one or more) was actually intended, causing a pattern to match an empty string where at least one character should have been required.
  2. Writing a greedy quantifier when a lazy one was needed, causing a pattern to match far more text than intended -- like matching everything between the first and LAST quotation mark instead of the nearest pair.
  3. Forgetting that {n,m} without a comma (just {n}) means exactly n repetitions, not "up to n".
Chapter Summary
  • * matches zero or more, + matches one or more, and ? matches zero or one of the preceding element.
  • {n}, {n,}, and {n,m} match exactly n, at least n, or between n and m repetitions respectively.
  • By default quantifiers are greedy (matching as much as possible); adding ? after them makes them lazy (matching as little as possible).
Browser Support

All standard regex quantifiers are supported in every modern browser and have been part of JavaScript regex since its earliest implementation.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.