Do these 3 things before closing this tab:
1Clear out junk files and repair common Windows errors2Scan for outdated or missing drivers - takes under a minute3Repair Windows errors before they cause bigger problemsIn JavaScript, a greedy quantifier tries to repeat its preceding atom as many times as it can; a lazy (non-greedy) quantifier tries as few times as possible. The rest of the pattern still has to match, so a greedy quantifier may give characters back and a lazy one may take more. The difference is a preference in how repetition is tried—not a rule that the final match must always be longest or shortest.
What changes when you add ? to a quantifier?
Quantifiers such as *, +, ?, and {n,m} specify how often the preceding atom may repeat. In JavaScript, these quantifiers are greedy by default: they try more repetitions first. Put a second question mark after the quantifier—such as *?, +?, ??, or {n,m}?—and it becomes lazy: it tries fewer repetitions first, then takes more if needed for the rest of the pattern to match. MDN’s JavaScript quantifier reference describes these forms.
As an Amazon Associate I earn from qualifying purchases.
The question mark changes the order of attempts, not what characters the quantified atom is allowed to match. For example, .* and .*? can both match characters permitted by the dot; they differ in how many they try to consume before checking what comes next.
Recommended Free Tools
Why does .* match too much?
Consider the JavaScript input <foo> <bar>:
| Pattern | Result | Reason |
|---|---|---|
/<.*>/ |
<foo> <bar> |
.* tries to consume as much as it can while leaving a closing > for the pattern. The last closing bracket allows the whole pattern to succeed. |
/<.*?>/ |
<foo> |
.*? starts with fewer characters and stops at the first closing bracket that lets the pattern succeed. |
Read each pattern from left to right: < matches the opening delimiter, .* or .*? matches repeated characters, and > requires a closing delimiter. MDN’s JavaScript regular-expression syntax guide documents this delimiter example and the role of quantifiers.
#1 Best Overall
Why can a greedy quantifier give characters back?
A quantifier’s preference is not the only constraint. The engine must also match the tokens that follow it. In a backtracking engine, a greedy quantifier first takes more repetitions; if the later part fails, it can give some back and try again. A lazy quantifier does the reverse: it starts with fewer repetitions and expands when the suffix cannot yet match.
For example, with a pattern that requires a particular letter after .*, the greedy quantifier may initially pass that letter, then backtrack until the required letter can match. With .*?, the engine may first try the suffix too early, then consume more text until the suffix succeeds. In both cases, judge the complete pattern rather than the quantifier alone.
Rank #2
- Used Book in Good Condition
How to diagnose an unexpected match
- Find the starting position. Identify where the engine begins looking for a match. In JavaScript, once a match succeeds at a starting position, the engine does not keep searching later positions solely to find a longer overall match.
- Identify the repeated atom. A quantifier applies only to the atom immediately before it. To repeat a larger expression, group it—for example, use
(ab)+to repeat the two-character sequenceab. - Read the required suffix. Work out what must match after the quantifier, and check where that suffix can first succeed.
- Trace the attempts. For a greedy quantifier, consider the most repetitions first and then any give-back needed. For a lazy quantifier, consider the fewest first and then any expansion needed.
- Check the engine and flags. Syntax and selection behavior can vary by regex flavor, and flags can affect which characters tokens such as the dot match. Verify the behavior for the language and version you are using.
Does this work the same way in every language?
The greedy-versus-lazy distinction exists in several common regex flavors, but not every implementation shares all syntax or match-selection details. The Python 3.14.8 re documentation describes greedy and non-greedy forms, while the Python regular-expression HOWTO explains their behavior. The Java SE 20 Pattern reference documents greedy quantifiers for Java’s API. Treat examples as flavor-specific: when a result matters, consult the documentation for the exact engine and version rather than assuming all regex implementations behave identically.
Lazy is not possessive
Lazy quantifiers still allow the engine to try more repetitions when the rest of the pattern needs them. Python 3.11 added possessive quantifiers, a different feature: they do not backtrack. As a result, a possessive quantifier can make a pattern fail where an ordinary greedy quantifier could give characters back. Python’s official re reference documents this distinction.
Rank #3
When should you use .*??
Use a lazy quantifier when you want the pattern to try the nearest qualifying boundary first—for example, the first closing delimiter in a simple string. Use a greedy quantifier when taking as much as possible before the required suffix is the intended preference. Neither form, by itself, guarantees that the match is well-formed or structurally meaningful; the surrounding pattern and input determine what succeeds.
If the delimiters can contain nested structures or escaped closing characters, a simple .*? may stop at a delimiter that is not the true end of the structure. In that situation, design the pattern around the actual allowed content, or use a parser when the input has nested syntax.
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




