fjordtools / Errors / Regex: nothing to repeat

SyntaxError: Invalid regular expression: Nothing to repeat

A quantifier such as *, + or ? repeats the item right before it, and the engine found nothing there. The pattern starts with a quantifier, or two quantifiers follow each other. Put an item before it, or escape the character with a backslash if you meant it literally.

Fjordtools team · Seen in JavaScript (V8, new RegExp) · Published · 1 min read

How browsers word it

Chrome and Node (V8)Invalid regular expression: /*abc/: Nothing to repeat

Common causes

  1. The pattern starts with a quantifier

    Someone wrote a shell wildcard such as *.txt, where * means anything. In a regex it is a quantifier and needs something before it.

    Input

    *abc

    Regex Tester says

    Invalid regular expression: /*abc/: Nothing to repeat
  2. Two quantifiers in a row

    a** repeats a repeat. To make a repeat lazy, add ? after it (a*?), but nothing else may follow a quantifier directly.

    Input

    a**

    Regex Tester says

    Invalid regular expression: /a**/: Nothing to repeat
  3. A quantifier after an anchor

    ^ and $ match positions, not characters, so they cannot be repeated.

    Input

    ^*

    Regex Tester says

    Invalid regular expression: /^*/: Nothing to repeat

How to fix it

  1. For any characters use .* (dot, then star), not a bare *.
  2. Escape a literal quantifier character: \* or \+ or \?.
  3. Remove the doubled quantifier, or add ? only to make a repeat lazy.
  4. Paste the pattern into the regex tester: it reports the error as you type.

Related errors

More error explainers are coming

See all errors.