Skip to content
FreeToolsKit.app

Text & Writing guide

Why Your Regex Matches Too Much: Greedy vs. Lazy Quantifiers

8 min read · Updated 20 November 2026 · Uses Regex Tester & Explainer

Quick answer

Your pattern matches too much because quantifiers such as *, + and {2,} are greedy. In JavaScript, "Quantifiers are greedy by default, which means they try to match as many times as possible" (MDN: Quantifier). So <.*> doesn't stop at the first >. It runs to the last > on the line.

There are three fixes. Pick the one that matches what went wrong:

  • Make the quantifier lazy: add ?, as in <.*?>. It then matches as few characters as it can.
  • Say what can't appear: use a negated character class, as in <[^>]+>. This is usually the better fix when a single character ends the item.
  • Anchor the pattern: use ^, $ or \b when the problem is where a match starts or ends, such as a match inside a longer number.

Two related traps:

  • Line breaks: the dot doesn't match a line break unless you add the s flag.
  • Nested quantifiers: patterns such as (a+)+ can take so long on text that almost matches that they look frozen. That's catastrophic backtracking.

You can test all of this in FreeToolsKit's Regex Tester & Explainer. Its Explanation labels every quantifier "as many as possible (greedy)" or "as few as possible (lazy)".

Regex Tester & ExplainerFree to use · No account required · Pattern and text stay on your deviceOpen Regex Tester & Explainer

How this guide tested it

Every result below comes from the tool, run in Chromium on short, made-up texts. Node.js 22's RegExp gave the same matches.

  1. Type the pattern in Regular expression pattern, without the surrounding slashes.
  2. Keep g Global ticked. It's on by default, and it lists every match instead of only the first.
  3. Paste the text into Test text. The test runs automatically.
  4. Read the status line, such as "4 matches", then each entry under Match details, which shows the matched text and its "Start · End · Length".

The first text is one line: <b>Price</b> and <i>Size</i> (28 characters).

Diagnose: one long match instead of several

With <.*> the tool reported 1 match: the whole line, <b>Price</b> and <i>Size</i>, Start 0, End 28.

That's the sign of a greedy over-match: fewer matches than you expected, and one of them is long. Here's what the engine did:

  1. < matched the first <.
  2. .* took every character to the end of the line.
  3. The final > then had nothing left to match, so the engine gave back characters one at a time until a > fitted. The first one it reached, counting back from the end, was the last > on the line.

That's how regular-expressions.info explains it: "the plus is greedy", and "Only if that causes the entire regex to fail, will the regex engine backtrack" (regular-expressions.info: Repetition with Star and Plus). MDN shows the same effect with square brackets (MDN: Quantifiers guide).

Fix 1: make the quantifier lazy

A lazy quantifier "will try to match as few times as possible, only matching more times if it's impossible to match the rest of the pattern" (MDN: Quantifier). Each greedy quantifier has a lazy form: *?, +?, ?? and {2,}?.

With <.*?> the tool reported 4 matches:

  • <b>: Start 0, End 3
  • </b>: Start 8, End 12
  • <i>: Start 17, End 20
  • </i>: Start 24, End 28

The Explanation described .*? as "Any character except a line break", repeated "zero or more times, as few as possible (lazy)".

Fix 2: use a negated character class

<[^>]+> means "a <, then one or more characters that aren't >, then >". On the same line it also gave 4 matches, with the same text and positions as the lazy version.

regular-expressions.info prefers this version. With the lazy plus, "the engine has to backtrack for each character", whereas with the negated character class "no backtracking occurs at all" on well-formed tags (regular-expressions.info: Repetition with Star and Plus). It also says exactly what you mean: the item ends at the first >.

Quoted strings work the same way. On Say "yes" or "no" and "" for blank:

  • ".*": 1 match, "yes" or "no" and "".
  • ".*?": 3 matches: "yes", "no" and the empty "".
  • "[^"]*": the same 3 matches.
  • "[^"]+": 2 matches. It skips the empty "", because + needs at least one character between the quotes.

So choose * or + by whether an empty value should count.

Line breaks: the dot stops at a newline unless you add s

The second text has two paragraphs over three lines. The first paragraph is split across a line break:

  • line 1: <p>Order 1042
  • line 2: ships Monday</p>
  • line 3: <p>Order 1043 ships Friday</p>

The tool's results:

  • <p>.*</p>, g: 1 match, only the one-line paragraph on line 3. The first paragraph was silently missed.
  • <p>.*?</p>, g: the same single match. Lazy doesn't help, because the dot can't cross the line break at all.
  • <p>.*</p>, g and s: 1 match of all 61 characters, from the first <p> to the last </p>. With s Dot matches newline ticked, the greedy over-match came back across lines.
  • <p>.*?</p>, g and s: 2 matches, one per paragraph. This is the fix.
  • <p>[^<]*</p>, g, no s: 2 matches as well. A negated character class matches line breaks, because a newline isn't <.

MDN describes the s flag as "Allows . to match newline characters" (MDN: Regular expressions guide). Don't confuse it with m Multiline, which changes only where ^ and $ match.

Where the negated class fails. On <p>A <b>bold</b> note</p>, <p>[^<]*</p> found nothing ("Valid pattern — no matches"), because [^<]* stops at the < of <b>. <p>.*?</p> found the whole paragraph. When the end marker is several characters long, such as </p>, a lazy quantifier is the simpler fix.

Fix 3: anchor the pattern

Sometimes the matches are the right length but appear in the wrong places. On Orders 1042, 10425 and 1043, \d{4} gave 3 matches: 1042, 1042 again (the first four digits of 10425, Start 13) and 1043.

Anchors fix this. They match a position, not a character:

  • \b (word boundary): \b\d{4}\b gave 2 matches, 1042 and 1043. A word boundary is a position, and "the length of a matched word boundary is zero" (MDN: Regular expression syntax cheat sheet).
  • ^ and $: these match the start and end of the input, and also at line breaks when the m flag is on. Use them to check a whole value. ^\d{4}$ matched 1042 and found no match in 10425.

When the test stops: catastrophic backtracking

The tool can also show a different problem: a pattern that runs so long it never finishes. ^(a+)+$ looks harmless, but it nests one quantifier inside another. When the text almost matches, the engine tries every way of splitting the letters between the inner and outer + before it gives up.

OWASP counts 16 possible paths for aaaaX and 65,536 for 16 letters followed by X, and "the number is double for each additional a" (OWASP: Regular expression Denial of Service).

The test used runs of the letter a ending in X:

  • Up to 28 letters: the tool answered "Valid pattern — no matches". At 28 letters that took about 1.6 seconds.
  • 29 letters and more: the tool showed "Stopped after 2 seconds", with "This pattern took too long on this text, so the test was stopped and its worker shut down. That's a processing limit, not a syntax error: the pattern may be valid."
  • Node.js 22 timings: each extra letter roughly doubled the time. 26 letters took 0.33 seconds, 27 took 0.66, 28 took 1.31 and 29 took 2.66.

The limit was 29 letters on the test machine in three repeated runs. On a faster or slower device it may come a letter or two earlier or later.

The fix: remove the nesting. ^a+$ accepts the same strings, because "one or more groups of one or more a's" is just "one or more a's". On the 34-letter text it answered "Valid pattern — no matches" straight away. Node checked 99,999 letters followed by X in under a millisecond.

The same idea fixes real patterns. regular-expressions.info shows ^(.*?,){11}P slowing down as fields are added, and replaces it with ^([^,\r\n]*,){11}P, which "needs just 61 steps to fail" (regular-expressions.info: Catastrophic Backtracking). Again, a negated character class replaces a dot.

No possessive quantifiers or atomic groups. Other regex flavours have tools that stop backtracking:

  • Possessive quantifiers such as *+.
  • Atomic groups such as (?>…). Python has had both since 3.11 (Python: re module).

JavaScript isn't among the flavours that regular-expressions.info lists as supporting possessive quantifiers (regular-expressions.info: Possessive Quantifiers). That's an inference from the list, not a statement on that page. In the test, the tool rejected "[^"]*+" with "Nothing to repeat" and (?>"[^"]*") with "Invalid group", and so did Node. In JavaScript, rewrite the pattern instead.

The 2-second limit belongs to this tool. The code runs each test in a background worker and shuts the worker down after 2 seconds, so the page stays responsive. Some engines have their own option: .NET, for example, lets you set a match time-out (Microsoft Learn: Backtracking in .NET regular expressions). Fix the pattern itself rather than relying on a limit.

Which fix to use

  • One character ends the item (>, ", a comma): use a negated character class such as [^>]+. It needs no backtracking on well-formed input, and it crosses line breaks.
  • A sequence of characters ends the item (</p>, -->): use a lazy quantifier, .*?. Add s if the item can span lines.
  • Matches turn up inside longer words or numbers: add \b, or ^ and $ to check a whole value.
  • The test stops after 2 seconds: look for a quantifier inside a repeated group, such as (a+)+ or (.*)*. Make the inner part more specific, or remove the nesting.

Check before you use the pattern

  1. Count: with g Global on, the number of matches is the number you expected.
  2. Positions: each match's Start and End cover only the item you wanted, not the gap between two items.
  3. Edge cases: you've tried a line break inside an item, an empty value and a nested tag, if your real text can contain them.
  4. A near miss: you've tried a long string that almost matches, such as an item with no closing character. A slow pattern shows up there first.
  5. Flavour: you'll use the pattern in JavaScript, or you've checked the other language's syntax. The tool tests JavaScript patterns only.