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

Greedy quantifiers keep looking until the condition is not satisfied. They match as much as possible.

Quantifiers are greedy by default.

Lazy quantifiers

Lazy quantifiers stop looking when the condition is satisfied. They match as little as possible.

Quantifiers can be made lazy by appending a question mark.

Table of quantifiers

Greedy quantifier Lazy quantifier Description
* *? zero or more
+ +? one or more
? ?? zero or one
{n} {n}? exactly n
{n,} {n,}? n or more
{n,m} {n,m}? between n and m

Example: getting the name of an HTML tag

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

// Greedy
html.match(/<.+>/)[0]
//=> '<div>foo</div>'

// Lazy
html.match(/<.+?>/)[0]
//=> '<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!

Alternative

Sometimes an alternative is to use a negated character class:

html.match(/<[^>]+>/)[0]
//=> '<div>'

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