Regex Greedy vs Lazy Quantifiers – Compare .* and .*?

Local only

See how a greedy quantifier can span multiple delimiters and how a lazy quantifier stops at the earliest possible match.

20 chars · 1 line

8 chars · 1 line

Pattern did not match the inputFlags: g

Regex quantifier behavior

How to compare greedy and lazy regex quantifiers

Greedy quantifiers consume as much text as possible while still allowing the pattern to succeed. That is why <.*> spans from the first < to the final > in the sample. Adding ? makes the quantifier lazy so Match All stops at the earliest closing delimiter each time. This tool is free to use. No account or payment is required.

Last updated

What you can do here

  • Greedy/lazy comparison
  • Match All output
  • Quantifier debugging
  • JavaScript RegExp semantics
  • Local sample testing
  • Browser-local processing

Steps

  1. Run Match All with the greedy quantifier against an input containing more than one possible endpoint.
  2. Add ? after the quantifier, such as .*?, when the earliest endpoint is the desired one.
  3. Retest nested or malformed input before relying on regex for structured markup parsing.

Lazy means shortest successful match, not a different wildcard

The dot still has the same character semantics. The extra ? changes how the quantifier expands while the engine searches for a successful match.

Greedy and lazy example

The greedy form spans all tags; the lazy form stops at each nearest >.

Greedy pattern

<.*>

Lazy pattern

<.*?>

Common questions

Frequently asked questions

What makes a quantifier lazy in JavaScript?

Place ? after the quantifier, for example *?, +?, ??, or {m,n}?.

Is lazy always better than greedy?

No. Choose the form whose stopping behavior matches the data and test representative edge cases.

Should regex parse arbitrary HTML?

For general HTML parsing, a parser is usually more appropriate. This example is about quantifier behavior with simple delimiters.

Debugging a specific Regex issue? Browse Regex troubleshooting.