Crashes, No Sound, or Screen Glitches?
Random freezes, missing sound and display glitches usually trace back to one bad driver. Find and replace yours safely.Free scan · under a minutePC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more
In JavaScript, regex quantifiers are greedy by default: they try to repeat their preceding atom as many times as possible. Add a second question mark after a quantifier—such as changing .* to .*?—and it tries fewer repetitions first. The rest of the pattern still has to match, so the final result depends on the whole expression, not just whether its quantifier is greedy or lazy.
What greedy and non-greedy quantifiers mean
Quantifiers such as *, +, ?, and {n,m} specify how many times the preceding atom may repeat. In JavaScript, they are greedy by default: they try the greatest permitted number of repetitions first. Adding a second ? makes the quantifier non-greedy, or lazy; it tries the fewest repetitions first and takes more only if the rest of the pattern cannot match otherwise. MDN documents these JavaScript forms in its quantifier reference.
This changes the quantifier’s preference, not what characters it is allowed to match. For example, .* and .*? can both match any characters allowed by the dot in the active regex flavor; they differ in how many repetitions they try before testing what follows.
Quick wins for a faster PC:
Clear out junk files and repair common Windows errorsFree Scan →Scan for outdated or missing drivers - takes under a minuteDriver Scan →Repair Windows errors before they cause bigger problemsFix Now →Why .* can match across several delimiters
Consider the JavaScript input <foo> <bar>:
| Pattern | What it matches | Why |
|---|---|---|
/<.*>/ |
<foo> <bar> |
.* tries to consume as much as possible, including the first closing angle bracket. The final > can then match the last closing bracket. |
/<.*?>/ |
<foo> |
.*? tries the fewest characters that allow the required closing > to match. |
Read each expression in parts: < matches the opening delimiter, .* or .*? matches repeated characters, and the final > is still required. The JavaScript regex syntax guide describes quantifiers as applying to the preceding atom. To repeat a larger expression as one unit, group it first, as in (ab)+. See MDN’s JavaScript regular-expression syntax cheat sheet.
#1 Best Overall
Why a greedy quantifier can give characters back
“Greedy” does not mean that the completed match is always the longest substring possible. It describes what the quantified part tries first. A regex engine must still satisfy the tokens after that quantifier. If the suffix cannot match after the quantifier has consumed as much as it can, a backtracking engine can give up some repetitions and try the suffix again.
For instance, with JavaScript input <foo> <bar>, the pattern /<.*>foo/ cannot complete a match: after the opening <, there is no later > followed by foo. Greediness cannot override that required suffix. When assessing a pattern, identify where a match can begin, what the quantifier repeats, and whether the required suffix can match at a candidate boundary.
Rank #2
- Used Book in Good Condition
Why a lazy quantifier can expand
Lazy means “try fewer first,” not “never take more.” If the suffix fails at the earliest candidate boundary, a backtracking engine can let a lazy quantifier consume additional characters and test the suffix again. Thus, a lazy quantifier may cover more text than the first closing delimiter when later tokens require it.
Likewise, a successful match at one starting position does not necessarily become longer just because a later starting position or another possible match could yield a different span. In JavaScript, once the engine finds a successful match at a starting position, it does not keep searching there solely to maximize the overall match. Match-selection rules are part of the regex flavor, not a universal longest-match rule.
Rank #3
How the behavior varies across regex flavors
Greedy quantifiers and lazy forms are available in multiple regex flavors, but syntax and match-selection details should not be assumed to be universal.
- JavaScript: MDN documents greedy quantifiers and their lazy forms; JavaScript’s result depends on how the complete pattern succeeds at a starting position. See the MDN quantifier reference.
- Python: Python 3.14.8’s
redocumentation describes ordinary quantifiers as greedy and provides lazy forms. It also documents possessive quantifiers, introduced in Python 3.11. A possessive quantifier does not backtrack, so a pattern can fail where an ordinary greedy quantifier could give characters back. Possessive is not another word for lazy. See the Python 3.14.8rereference and Python regular-expression HOWTO. - Java: The Java SE 20
PatternAPI documents greedy quantifiers for its own regex implementation. Treat that as Java-specific documentation rather than proof that every engine behaves identically. See Oracle’s Java SE 20Patternreference.
When to use .*?—and when not to
Use a lazy quantifier when the pattern should prefer the nearest boundary that lets the rest of the expression succeed, such as the first closing angle bracket in the example. It is not a substitute for understanding the delimiters or the suffix: if the suffix requires a later boundary, the lazy quantifier may expand.
For structured text such as HTML or nested delimiters, a broad dot expression may not express the structure you intend. The example demonstrates quantifier behavior; it is not a general-purpose parser for nested markup. Choose a pattern or parser that matches the actual structure and the regex flavor you are using.
Quick Recap
Best Value
A practical way to diagnose an unexpected match
- Name the regex flavor. Confirm whether the pattern is being run by JavaScript, Python, Java, or another engine; syntax and selection behavior can differ.
- Mark the atoms and quantifiers. Identify exactly which preceding atom each quantifier modifies. Add parentheses if a quantifier should repeat a larger expression.
- Read the suffix. Treat everything after the quantifier as a requirement. Ask whether it can match at the earliest boundary for a lazy quantifier or after the greatest initial consumption for a greedy one.
- Trace backtracking or expansion. If the suffix fails, determine whether the engine can return characters to a greedy quantifier or add characters to a lazy one.
- Check the returned match, not just the quantifier’s first preference. The match begins at an engine-selected position, and flavor-specific selection rules determine which successful result is returned.
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.

