Regex Quantifier Reference & Demo

A clear reference for regular-expression quantifiers*, +, ?, and the {n,m} forms — plus the crucial difference between greedy and lazy matching. Click any entry to load its example into the live demo, type your own text, and watch exactly how much each quantifier grabs. Everything runs in your browser.

Matches highlighted

Quantifier reference — click a row to try it

How to use the Regex Quantifier Reference & Demo

Type a regular expression in the Pattern box and some text in the Test text box; every match is highlighted live below, with a count. Matching is global so you can see how a quantifier behaves across the whole string, not just at the first match. The default <.+> against text with two tags is the classic demonstration of greedy matching — notice how the single match stretches from the first < all the way to the last >, swallowing everything between.

The reference table lists each quantifier with its meaning and a ready-made example. Click a row to load that example into the pattern box and watch it run against your text. Compare the greedy and lazy rows directly: <.+> grabs as much as it can, while <.+?> — the same pattern with a ? added — grabs as little as possible and so matches each tag separately. Try the counted forms too: \d{3} for exactly three digits, \d{2,4} for two to four, and \d{2,} for two or more.

It all runs in your browser, so matching is instant, works offline, and nothing you paste is uploaded anywhere.

Quantifiers, and greedy versus lazy

A quantifier in a regular expression says how many times the thing before it may repeat. Without one, each token matches exactly once; add a quantifier and you can match runs of characters of varying length. There are only a handful to learn. The three single-character quantifiers are * (zero or more), + (one or more), and ? (zero or one — that is, optional). The braced forms give precise counts: {n} matches exactly n times, {n,} matches n or more, and {n,m} matches between n and m times inclusive. So \d{3} is exactly three digits, \d{2,4} is two to four, and colou?r matches both "color" and "colour" because the u is optional. A quantifier attaches to whatever immediately precedes it — a single character, a character class like [a-z], an escape like \d, or a whole group in parentheses — so (ab)+ repeats the pair "ab".

The part that trips people up is greediness. By default every quantifier is greedy: it matches as much as it possibly can while still letting the overall pattern succeed. The textbook example is <.+> applied to <a> and <b>. You might expect it to match <a>, but .+ first gobbles the entire rest of the line, then backtracks — gives characters back one at a time — until the final > can match, which lands on the very last >. The result is one match covering <a> and <b>. Adding a ? after the quantifier makes it lazy (also called reluctant): <.+?> matches as little as possible, so it stops at the first > and yields two separate matches, <a> and <b>. Every quantifier has a lazy twin — *?, +?, ??, and {n,m}? — and choosing the right one is often the difference between a pattern that works and one that quietly matches too much.

A third, less common mode is possessive quantifiers, written with a trailing +*+, ++, ?+, {n,m}+. They match greedily but refuse to backtrack at all, which can make a pattern faster and prevent certain failures, though they are supported in engines like PCRE, Java, and Ruby but not in standard JavaScript. Greediness also explains the performance trap known as catastrophic backtracking: nesting quantifiers, as in (a+)+, can force the engine to try an exponential number of ways to split the input on a near-miss, freezing on surprisingly short strings. The cures are to avoid overlapping nested quantifiers, prefer specific classes like [^>] over a broad ., and reach for lazy matching when you only want the nearest delimiter. Seeing a quantifier highlight its match live, and toggling between its greedy and lazy forms, is the quickest way to build an accurate intuition for all of this.

Common use cases

  • Learning regex. See greedy versus lazy matching highlighted side by side in real text.
  • Debugging "matches too much". Discover that a greedy .+ is the culprit and switch to lazy.
  • Building patterns. Dial in exact repetition counts with {n}, {n,}, and {n,m}.
  • Teaching. Demonstrate quantifier behaviour interactively without setting up any tooling.

Frequently asked questions