Non-greedy quantifier
A non-greedy quantifier matches the fewest repetitions that allow the entire regular expression to match.
Secrets of the JavaScript Ninja, Third Edition
A non-greedy operator is a regular-expression quantifier that initially matches as little text as possible while allowing the pattern to match.
- How it is used
- Selects the first suitable stopping point when matching repeated characters in delimited text.
- How deep it goes
- Defines the ? suffix and demonstrates its effect, then contrasts greedy backtracking without tracing non-greedy execution in detail.
- Examples
- Contrasts /<.*>/ with /<.*?>/ on two angle-bracketed fragments and compares greedy and non-greedy letter matching.
- What you can do after
- Apply it via JavaScript regular expressions by appending ? to a repetition operator when the earliest suitable match is wanted.
Read how Secrets of the JavaScript Ninja, Third Edition explains non-greedy quantifier →
The Well-Grounded Rubyist, Fourth Edition
A non-greedy quantifier matches the fewest repetitions needed for the remainder of the regular expression to match.
- How it is used
- Limits how far a quantified pattern extends while allowing the remainder of the expression to match.
- How deep it goes
- Defines `*?` and `+?`, contrasts greedy matching, and explains backtracking with a digit example.
- Examples
- Demonstrates stopping at the first exclamation point, matching digits, and capturing parenthesized text across newlines.
- What you can do after
- Apply `*?` or `+?` in Ruby regular expressions to limit a match while satisfying the remaining pattern.
Read how The Well-Grounded Rubyist, Fourth Edition explains non-greedy quantifier →
Also covered in
- Tiny Python Projects: Learn coding and testing with puzzles and games
- Regular Expression Puzzles and AI Coding Assistants
What to know first
- regular expressionA regular expression is a formal pattern notation that describes sets of strings and specifies how text can be matched, searched, or transformed.
- regex groupingA regex capturing group is a parenthesized regular-expression subpattern whose matched text is retained for separate access or reference.
- lookbehind assertionA lookbehind assertion is a regular-expression assertion that checks whether a pattern matches before the current position without consuming the checked input.
Related concepts
- greedy matchingcontrasts with · Greedy matching is regular-expression behavior in which a quantifier matches as much text as possible while still allowing the overall pattern to match.
- quantifierkind of · A regular-expression quantifier is syntax that specifies how many times a preceding pattern element may or must match.
- telomereaddresses · A telomere is a specialized repetitive DNA sequence at the end of a chromosome or other DNA sequence.
- catastrophic backtrackingcontrasts with · Catastrophic backtracking is pathological regular-expression matching in which ambiguous alternatives cause exponentially many paths to be explored, potentially causing extreme execution time.
- regular expressionused by · A regular expression is a formal pattern notation that describes sets of strings and specifies how text can be matched, searched, or transformed.
- regex engineused by · A regular-expression engine is software that interprets regular-expression patterns and applies them to text for matching, searching, splitting, or transformation.
Questions about non-greedy quantifier
What is non-greedy quantifier?
A non-greedy quantifier matches the fewest repetitions that allow the entire regular expression to match.
Which Manning books explain non-greedy quantifier?
Secrets of the JavaScript Ninja, Third Edition and The Well-Grounded Rubyist, Fourth Edition.
What should I know before learning non-greedy quantifier?
Manning books that teach non-greedy quantifier usually introduce regular expression, regex grouping and lookbehind assertion first.
What is the difference between non-greedy quantifier and greedy matching?
A non-greedy quantifier matches the fewest repetitions that allow the entire regular expression to match. Greedy matching is regular-expression behavior in which a quantifier matches as much text as possible while still allowing the overall pattern to match.
What is non-greedy quantifier a kind of?
Non-greedy quantifier is a kind of quantifier. A regular-expression quantifier is syntax that specifies how many times a preceding pattern element may or must match.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.

