Greedy vs Lazy
By default every quantifier is greedy: it grabs as many characters as it can, and only gives some back if the rest of the pattern would otherwise fail. Most of the time that is fine — but when the same delimiter appears several times in a line, greediness produces surprising results.
The classic surprise
You want each HTML tag in a line, so you write <.+> — "a <, some characters, a >":
/<.+>/g
<p>Hello <b>world</b></p>1 match
#1 "<p>Hello <b>world</b></p>" at index 0
One giant match! Here is what the engine did:
<matched the first<..+swallowed everything to the end of the line.>now had nothing left to match, so the engine backtracked:.+gave back one character at a time until>could match — which happened at the last>in the line.
Lazy quantifiers
Put a ? after a quantifier to make it lazy (also called reluctant or non-greedy). A lazy quantifier matches as few characters as possible and only takes more when the rest of the pattern fails.
| Greedy | Lazy | Meaning of the lazy form |
|---|---|---|
* |
*? |
0 or more, as few as possible |
+ |
+? |
1 or more, as few as possible |
? |
?? |
0 or 1, prefer 0 |
{n,m} |
{n,m}? |
n to m, prefer n |
/<.+?>/g
<p>Hello <b>world</b></p>4 matches
#1 "<p>" at index 0
#2 "<b>" at index 9
#3 "</b>" at index 17
#4 "</p>" at index 21
Now .+? takes one character, checks whether > follows, takes another if not, and stops at the first >.
Often better: a negated class
Lazy quantifiers work, but a negated class states your intent more precisely: "a <, then any characters that are not >, then >". It cannot run past a > at all, so no backtracking is needed:
/<[^>]+>/g
<p>Hello <b>world</b></p>4 matches
#1 "<p>" at index 0
#2 "<b>" at index 9
#3 "</b>" at index 17
#4 "</p>" at index 21
Tip: Prefer
"[^"]*"over".*?"when the delimiter is a single character. It is faster, and it behaves the same whether or not thesflag is on.
Lazy does not mean "shortest possible match overall"
The engine still returns the leftmost match first. Laziness only decides how far a quantifier extends from where the match started:
/a.*?b/
aaab1 match
#1 "aaab" at index 0
The match starts at index 0 (the first position where a match is possible), even though ab at index 2 is shorter.
Greedy vs lazy when extracting
A quoted-string example shows the difference clearly:
/'(.*)'/
say 'hi' and 'bye'1 match
#1 "'hi' and 'bye'" at index 4
group 1: "hi' and 'bye"
/'(.*?)'/g
say 'hi' and 'bye'2 matches
#1 "'hi'" at index 4
group 1: "hi"
#2 "'bye'" at index 13
group 1: "bye"
In code
const md = "Use **bold** and **more bold** here";
console.log(md.replace(/\*\*(.+)\*\*/g, "<b>$1</b>"));
console.log(md.replace(/\*\*(.+?)\*\*/g, "<b>$1</b>"));Use <b>bold** and **more bold</b> here
Use <b>bold</b> and <b>more bold</b> here
Note: Regex is not a full HTML parser — nested or malformed markup will beat it. Use these patterns for quick extraction from simple, known text, and a real parser (like the browser's
DOMParser) for real HTML.
Test yourself
What does /<.+>/ match in <a><b>?
<a><b><a><b>- No match
How do you make + lazy?
+!+??+(+)
Which pattern grabs one double-quoted string without using a lazy quantifier?
".*""[^"]*"".+"["].*