• JavaScript
  • JavaScript
  • Tutorials and guides
  • Beginner's tutorials
    1. JavaScript Guide
      1. Loops and iteration
      2. Representing dates & times
      3. Working with objects
      4. Iterators and generators
      5. Asynchronous JavaScript
      6. Equality comparisons and sameness
      7. Meta programming
      8. AggregateError
      9. AsyncGenerator
      10. BigInt
      11. DataView
      12. encodeURI()
      13. escape() Deprecated
      14. Float16Array
      15. Generator
      16. Int8Array
      17. InternalError Non-standard
      18. Iterator
      19. NaN
      20. parseInt()
      21. ReferenceError
      22. SharedArrayBuffer
      23. Temporal Experimental
      24. Uint8ClampedArray
      25. unescape() Deprecated
      26. WeakSet
  • Assignment (=)
  • Bitwise AND (&)
  • Bitwise OR assignment (|=)
  • Comma operator (,)
  • Destructuring
  • Exponentiation (**)
  • Greater than (>)
  • import.meta.resolve()
  • Inequality (!=)
  • Less than (<)
  • Logical NOT (!)
  • Multiplication assignment (*=)
  • Nullish coalescing assignment (??=)
  • Optional chaining (?.)
  • Right shift (>>)
  • Strict inequality (!==)
  • this
  • Unsigned right shift (>>>)
  • yield*
  • Block statement
  • continue
  • export
  • for...in
  • if...else
  • let
  • try...catch
  • with Deprecated
  • get
  • The arguments object
    1. callee Deprecated
    2. extends
    3. Static initialization blocks
  • Character class escape: \d, \D, \w, \W, \s, \S
  • Input boundary assertion: ^, $
  • Modifier: (?ims-ims:...)
  • Quantifier: *, +, ?, {n}, {n,}, {n,m}
  • Errors
    1. RangeError: argument is not a valid code point
    2. RangeError: invalid array length
    3. RangeError: repeat count must be less than infinity
    4. ReferenceError: assignment to undeclared variable "x"
    5. SyntaxError: 'arguments'/'eval' can't be defined or assigned to in strict mode code
    6. SyntaxError: \ at end of pattern
    7. SyntaxError: await is only valid in async functions, async generators and modules
    8. SyntaxError: continue must be inside loop
    9. SyntaxError: function statement requires a name
    10. SyntaxError: identifier starts immediately after numeric literal
    11. SyntaxError: invalid assignment left-hand side
    12. SyntaxError: invalid class set operation in regular expression
    13. SyntaxError: invalid property name in regular expression
    14. SyntaxError: invalid unicode escape in regular expression
    15. SyntaxError: missing ) after argument list
    16. SyntaxError: missing } after property list
    17. SyntaxError: missing variable name
    18. SyntaxError: numbers out of order in {} quantifier.
    19. SyntaxError: property name __proto__ appears more than once in object literal
    20. SyntaxError: rest parameter may not have a default
    21. SyntaxError: super() is only valid in derived class constructors
    22. SyntaxError: unlabeled break must be inside loop or switch
    23. TypeError: 'caller', 'callee', and 'arguments' properties may not be accessed
    24. TypeError: "x" is not a function
    25. TypeError: BigInt value can't be serialized in JSON
    26. TypeError: can't convert BigInt to number
    27. TypeError: can't redefine non-configurable property "x"
    28. TypeError: class constructors must be invoked with 'new'
    29. TypeError: Initializing an object twice is an error with private fields/methods
    30. TypeError: Iterator/AsyncIterator constructor can't be used directly
    31. TypeError: property "x" is non-configurable and can't be deleted
    32. TypeError: X.prototype.y called on incompatible type
    33. JavaScript technologies overview
    34. Strict mode
    35. Learn more
    36. See full compatibility
  • A quantifier repeats an atom a certain number of times. The quantifier is placed after the atom it applies to.

    Syntax

    regex
    / Greedy
    atom?
    atom*
    atom+
    atom{count}
    atom{min,}
    atom{min,max}
    
    / Non-greedy
    atom??
    atom*?
    atom+?
    atom{count}?
    atom{min,}?
    atom{min,max}?
    

    Parameters

    atom

    A single atom.

    count

    A non-negative integer. The number of times the atom should be repeated.

    min

    A non-negative integer. The minimum number of times the atom can be repeated.

    max Optional

    A non-negative integer. The maximum number of times the atom can be repeated. If omitted, the atom can be repeated as many times as needed.

    Description

    A quantifier is placed after an atom to repeat it a certain number of times. It cannot appear on its own. Each quantifier is able to specify a minimum and maximum number that a pattern must be repeated for.

    Quantifier Minimum Maximum
    ? 0 1
    * 0 Infinity
    + 1 Infinity
    {count} count count
    {min,} min Infinity
    {min,max} min max

    For the {count}, {min,}, and {min,max} syntaxes, there cannot be white spaces around the numbers — otherwise, it becomes a literal pattern.

    js
    const re = /a{1, 3}/;
    re.test("aa"); / false
    re.test("a{1, 3}"); / true
    

    This behavior is fixed in deprecated syntax for web compatibility, and you should not rely on it.

    js
    /a{1, 3}/u; / SyntaxError: Invalid regular expression: Incomplete quantifier
    

    It is a syntax error if the minimum is greater than the maximum.

    js
    /a{3,2}/; / SyntaxError: Invalid regular expression: numbers out of order in {} quantifier
    

    Quantifiers can cause capturing groups to match multiple times. See the capturing groups page for more information on the behavior in this case.

    Each repeated match doesn't have to be the same string.

    js
    /[ab]*/.exec("aba"); / ['aba']
    

    Quantifiers are greedy by default, which means they try to match as many times as possible until the maximum is reached, or until it's not possible to match further. You can make a quantifier non-greedy by adding a ? after it. In this case, the 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 with this many repetitions.

    js
    /a*/.exec("aaa"); / ['aaa']; the entire input is consumed
    /a*?/.exec("aaa"); / ['']; it's possible to consume no characters and still match successfully
    /^a*?$/.exec("aaa"); / ['aaa']; it's not possible to consume fewer characters and still match successfully
    

    However, as soon as the regex successfully matches the string at some index, it will not try subsequent indices, although that may result in fewer characters being consumed.

    js
    /a*?$/.exec("aaa"); / ['aaa']; the match already succeeds at the first character, so the regex never attempts to start matching at the second character
    

    Greedy quantifiers may try fewer repetitions if it's otherwise impossible to match the rest of the pattern.

    js
    /[ab]+[abc]c/.exec("abbc"); / ['abbc']
    

    In this example, [ab]+ first greedily matches "abb", but [abc]c is not able to match the rest of the pattern ("c"), so the quantifier is reduced to match only "ab".

    Greedy quantifiers avoid matching infinitely many empty strings. If the minimum number of matches is reached and no more characters are being consumed by the atom at this position, the quantifier stops matching. This is why /(a*)*/.exec("b") does not result in an infinite loop.

    Greedy quantifiers try to match as many times as possible; it does not maximize the length of the match. For example, /(aa|aabaac|ba)*/.exec("aabaac") matches "aa" and then "ba" instead of "aabaac".

    Quantifiers apply to a single atom. If you want to quantify a longer pattern or a disjunction, you must assertions.

    js
    /^*/; / SyntaxError: Invalid regular expression: nothing to repeat
    

    In deprecated syntax for web compatibility, and you should not rely on it.

    js
    /(?=a)?b/.test("b"); / true; the lookahead is matched 0 time
    

    Examples

    Removing HTML tags

    The following example removes HTML tags enclosed in angle brackets. Note the use of ? to avoid consuming too many characters at once.

    js
    function stripTags(str) {
      return str.replace(/<.+?>/g, "");
    }
    
    stripTags("<p><em>lorem</em> <strong>ipsum</strong></p>"); / 'lorem ipsum'
    

    The same effect can be achieved with a greedy match, but not allowing the repeated pattern to match >.

    js
    function stripTags(str) {
      return str.replace(/<[^>]+>/g, "");
    }
    
    stripTags("<p><em>lorem</em> <strong>ipsum</strong></p>"); / 'lorem ipsum'
    

    Warning: This is for demonstration only — it doesn't handle > in attribute values. Use a proper HTML sanitizer like the HTML sanitizer API instead.

    Locating Markdown paragraphs

    In Markdown, paragraphs are separated by one or more blank lines. The following example counts all paragraphs in a string by matching two or more line breaks.

    js
    function countParagraphs(str) {
      return str.match(/(?:\r?\n){2,}/g).length + 1;
    }
    
    countParagraphs(`
    Paragraph 1
    
    Paragraph 2
    Containing some line breaks, but still the same paragraph
    
    Another paragraph
    `); / 3
    

    Warning: This is for demonstration only — it doesn't handle line breaks in code blocks or other Markdown block elements like headings. Use a proper Markdown parser instead.

    Specifications

    Specification
    ECMAScript® 2026 Language Specification
    # prod-Quantifier

    Browser compatibility

    See also