DevLab
Regex

Regex Quantifiers: Complete Guide

Master greedy, lazy, and possessive quantifiers in regular expressions with practical examples.

Basic Quantifiers

Quantifiers specify how many times a pattern element should match. They apply to the character, group, or character class immediately before them.

  • * — Zero or more times
  • + — One or more times
  • ? — Zero or one time (optional)
  • {n} — Exactly n times
  • {n,} — n or more times
  • {n,m} — Between n and m times

Greedy Quantifiers (Default)

By default, quantifiers are greedy — they try to match as much text as possible while still allowing the overall pattern to match. Think of them as "try to grab everything, then give back what you must."

The pattern ".+" applied to "hello" "world" matches the entire string "hello" "world", not just "hello".

Lazy Quantifiers

Add ? after any quantifier to make it lazy: *?, +?, ??, {n,m}?. Lazy quantifiers try to match as little as possible.

The pattern ".+?" applied to "hello" "world" now matches "hello" and then "world" separately.

Practical Examples

HTML tag content: <[^>]+>(.*?)</ — The lazy .*? prevents matching across multiple tags.

Quoted strings: "[^"]*" — Better than ".+?" because it uses a negated character class, which is clearer and faster.

Optional whitespace: \s* — Zero or more spaces, useful for flexible parsing.

Practice with these tools

More Learning Topics

RegexRegex Basics: A Complete Beginner's GuideRegexRegex Special Characters: Complete ReferenceRegexRegex Groups and Captures ExplainedCSSCSS Selectors: The Complete GuideCSSCSS Specificity: Why Your Styles Aren't ApplyingJSONJSONPath Syntax: Query JSON Like XPathTimeUnix Timestamps ExplainedEncodingBase64 Encoding ExplainedEncodingJWT Structure and How It WorksEncodingJWT vs Session Tokens: Which Should You Use?EncodingJWT Refresh Tokens ExplainedCryptoHash Functions Explained: MD5, SHA-256, and When to Use EachEncodingURL Encoding Explained: What %20 Actually MeansJSONJSON Schema Explained: Validate Your JSON DataJSONJSON vs YAML: Which Should You Use?JSONJSON.stringify and JSON.parse: Edge Cases You Should KnowRegexRegex Lookahead and Lookbehind: Match Without ConsumingRegexRegex for Email Validation: The Right ApproachCSSThe CSS Box Model: margin, padding, border, and contentCSSFlexbox vs CSS Grid: When to Use EachCSSCSS Custom Properties (Variables) ExplainedTimeISO 8601 Explained: The Right Way to Format DatesTimeUnix Timestamps vs ISO 8601: Which to Use in Your API?EncodingUTF-8 Explained: How Computers Store TextTextCORS Explained: Why Your API Call is BlockedTextHTTP Status Codes: A Practical Developer GuideRegexNamed Capture Groups in Regex: Clean ExtractionsColorsColor Spaces Explained: RGB, HSL, HEX, and BeyondColorsColor Contrast for Developers: WCAG Rules and How to CheckNumbersNumber Bases Explained: Binary, Octal, Decimal, and HexNumbersBitwise Operations for Web DevelopersHTMLHTML Semantic Elements: A Complete ReferenceTextText Encoding for Developers: ASCII, UTF-8, and UnicodeRegexWhat is Regex? Complete Guide for DevelopersJSONJSON Format Explained: Structure, Syntax, and Common ErrorsEncodingHow JWT Works: Header, Payload, Signature DecodedHTMLHow to Convert Any Website to Markdown (for LLMs, RAG & Docs)TextPreparing Website Content for RAG: Clean Markdown Pipelines