Skip to content
Tutorials Center
My learning

Topics

Lesson 12 of 38 ยท Character Classes and Quantifiers

Greedy Quantifiers

Repeat patterns with the regex quantifiers ?, *, +, {n}, {n,} and {n,m}. Learn how greedy matching works, why it sometimes over-matches, and how to apply quantifiers to groups and classes.

A quantifier says how many times the token before it may repeat. Without quantifiers a regex could only match fixed-length text; with them you can describe "one or more digits", "an optional sign" or "between two and four letters".

The quantifier family

Quantifier Meaning Example Matches
? zero or one colou?r color, colour
* zero or more ab* a, ab, abbb
+ one or more \d+ 7, 2026
exactly n \d{4} 2026
{n,} n or more \d{2,} 12, 12345
{n,m} between n and m \d{2,4} 12, 123, 1234
Node.js Each quantifier

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

(JSON.stringify(undefined) prints nothing useful, so a missing match shows as undefined.)

Quantifiers apply to one token

A quantifier repeats the single token just before it: a character, a class, an escape or a group. To repeat several characters, group them.

Node.js Repeating groups

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The IPv4 pattern checks only the shape (it accepts 999.999.999.999); real range checking needs code or a much longer pattern.

Greedy by default

Quantifiers are greedy: they take as many characters as they can, and give some back only if the rest of the pattern would otherwise fail. This is the single most important fact about quantifiers.

Node.js Greed in action

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Here is the engine's thinking for <.+> on <b>bold</b>:

flowchart TD
  A["'<' matches the first <"] --> B[".+ grabs EVERYTHING to the end of the text"]
  B --> C["Needs '>' but the text ended: fail"]
  C --> D["Give back one character, try '>' again"]
  D --> E{"Is it '>'?"}
  E -- "no" --> D
  E -- "yes" --> F["Match ends at the LAST '>'"]

Greedy quantifiers take everything first and retreat step by step (this retreat is called backtracking).

Backtracking in miniature

You can observe how many characters the greedy part keeps:

Node.js How much does the star keep?

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

The greedy .* consumed abc1234 and left a single digit for \d+, because + only needs one. If you wanted all digits in group 2, greediness works against you. The fix is a lazy quantifier or, better, a more specific pattern such as ^(\D*)(\d+)$.

Zero-length matches

* and ? can match nothing, which sometimes gives surprising extra matches.

Node.js Empty matches

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

If you only want real matches, use + instead of *, or make sure the pattern has at least one required token.

Choosing the right quantifier

  • Use or {n,m} for known sizes: \d{4} for a year, [0-9a-f]{2} for a byte.
  • Use ? for optional parts: signs, prefixes, s for plurals.
  • Use + when at least one is required; * only when zero is meaningful.
  • Prefer specific classes over .*; they fail faster and over-match less.
Node.js Optional sign and decimals

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does /ab+/ match in 'abbbc'?

    The + applies only to b and is greedy, so it takes all three b characters.
  2. 2. Which quantifier means 'between 2 and 4 times'?

    Counted quantifiers use braces and a comma, {min,max}.
  3. 3. Why does /<.+>/ match the whole of '<b>bold</b>'?

    Greedy quantifiers consume the maximum and give back only when the rest of the pattern fails.
  4. 4. What does 'aab'.match(/a*/g) return?

    After matching aa, the star can still match the empty string at b and at the end of the text.

Summary

  • ?, *, +, , {n,} and {n,m} repeat the single token before them; use groups to repeat sequences.
  • Quantifiers are greedy: they take the most they can and backtrack only if needed.
  • * and ? can match the empty string, which produces extra empty matches with g.
  • Prefer precise classes like [^>]+ over .+ to avoid over-matching.

Next: Lazy Quantifiers and Backtracking Basics