Level 2: Practical patterns · Lesson 19 of 30
Greedy vs Lazy Quantifiers
Quantifiers are greedy by default and grab as much as they can. Adding ? makes them lazy so they stop at the first point where the rest of the pattern can match.
Quantifiers such as *, +, ? and {n,m} are greedy by default. A greedy quantifier matches as many characters as it possibly can, then gives some back only if the rest of the pattern would otherwise fail. This behaviour is usually what you want, but it surprises people when the pattern contains a wildcard like .*.
Take the text <b>bold</b> and the pattern <.+>. You might expect it to match <b>, but the .+ first runs all the way to the end of the line. Then the engine backtracks one character at a time until it finds a > that lets the pattern finish. The last > in the text is found first, so the match is the entire string <b>bold</b>.
A lazy quantifier does the opposite. Written by adding a question mark, as in *?, +?, ?? or {n,m}?, it matches as few characters as possible and grows only when the rest of the pattern fails. The pattern <.+?> matches <b> and then, on the next search, </b>. Each tag is matched separately.
Laziness changes the preferred length, not the set of possible matches. A lazy quantifier still has to produce a valid match, so \d{2,4}? on 2026 returns 20 because two digits is the minimum, and then 26 on the next attempt.
Often an even better fix than laziness is a negated character class. Instead of ".*?" to match a quoted string, you can write "[^"]*", which says 'any characters except a quote'. This expresses the rule directly, cannot run past the closing quote, and avoids extra backtracking. The same idea works for brackets with \[[^\]]*\] and for tags with <[^>]+>.
Use lazy quantifiers when the stopping point is a multi-character word or pattern that a character class cannot describe, such as start.*?end. Use negated classes when the stopping point is a single character. Both approaches are supported in every mainstream engine.
In the tester below, remove the ? after .+ and watch two separate matches merge into one long match.
Live tester
4 matches (JavaScript engine)
<b>bold</b> and <i>italic</i>
Examples
Numbers
/\d{2,4}?/Year 2026
Matches: “20”, “26”
The lazy range takes the minimum of two digits each time, so it returns `20` and `26`.
Text
/".*?"/He said "hello" and then "goodbye".
Matches: “"hello"”, “"goodbye"”
Each quoted word is matched separately instead of one long span from the first quote to the last.
Codes
/\[.*?\]/Tags [A1] [B2] [C3] attached.
Matches: “[A1]”, “[B2]”, “[C3]”
The lazy dot stops at the first closing bracket after each opening bracket.
Practice tasks
Your regex runs over the text with the global flag. It passes when it finds every required match and nothing that should be left out.
Task 1: Match each quoted word separately, including the quotes.
Set "red" then "blue" now
Task 2: Match every HTML tag on its own.
<p>Hi</p><br>
Task 3: Match from each `start` up to the nearest following `end`.
start one end start two end