Quantifiers in JavaScript

From the JavaScript Regular Expressions cheat sheet · Basic Patterns · verified Jul 2026

Quantifiers

Specifying repetition

javascript
*       0 or more
+       1 or more
?       0 or 1
{n}     Exactly n times
{n,}    n or more times
{n,m}   Between n and m times

// Greedy vs Lazy
*?      0 or more (lazy)
+?      1 or more (lazy)
??      0 or 1 (lazy)
{n,}?   n or more (lazy)

// Examples
/ab*c/.test("ac")        // true (0 b's)
/ab+c/.test("ac")        // false (needs 1+ b's)
/colou?r/.test("color")  // true (optional u)
/\d{3}/.test("123")      // true (exactly 3)
/\d{2,4}/.test("12345")  // true (matches "1234")

// Greedy vs Lazy
"<div>text</div>".match(/<.*>/)    // ["<div>text</div>"]
"<div>text</div>".match(/<.*?>/)   // ["<div>"]
💡 Greedy quantifiers match as much as possible
⚡ Add ? after quantifier for lazy (minimal) matching
📌 {n,m} is inclusive - matches n to m times
🟢 Use ? for optional parts like https? for http/https
quantifiersrepetition

More JavaScript tasks

Back to the full JavaScript Regular Expressions cheat sheet