Lazy and greedy quantifiers in regular expressions

Published on in Regular expressions

Last updated on

Greedy quantifiers (the default, e.g. *) match as much as possible. Lazy quantifiers (e.g. *?) match as little as possible.

Table of contents

Greedy quantifiers

Quantifiers are greedy by default. They keep looking until the condition is not satisfied.

These are greedy quantifiers:

  • * (zero or more)
  • + (one or more)
  • ? (zero or one)
  • {n} (exactly n)
  • {n,} (n or more)
  • {n,m} (between n and m)

Lazy quantifiers

Quantifiers can be made lazy by appending a question mark. They stop looking when the condition is satisfied.

These are lazy quantifiers (descriptions omitted because they are the same as above):

  • *?
  • +?
  • ??
  • {n}?
  • {n,}?
  • {n,m}?

Example: getting the name of an HTML tag

const html = '<div>foo</div>'

// Greedy
//=> '<div>foo</div>'

// Lazy
//=> '<div>'

As you can see, the greedy quantifier is too greedy in this case, whereas the lazy quantifier matches just enough but not too much. It's like smart laziness. Nice!

Sometimes an alternative is to use a negated character class:

//=> '<div>'

In this case the result is the same as when using a lazy quantifier, but the performance is better because "the negated character class can only match one way for a given input."