Skip to content

JavaScript error“SyntaxError: Invalid regular expression: /…/: Nothing to repeat”

The code stops with SyntaxError: Invalid regular expression, followed by the pattern and a reason such as Nothing to repeat, Unterminated group, Invalid escape or Invalid character class. Safari words the reason differently, for example nothing to repeat or missing ). The pattern is not valid JavaScript regex syntax, usually because text meant to be matched literally was used as a pattern, or because of stricter rules in the u and v modes.

Short answer

Look at the pattern in the message. If it came from user input or a filename, escape it before building the RegExp: RegExp.escape(text) in current browsers and recent Node.js, or a replace() fallback for older ones. If it is a form field’s pattern attribute, remember browsers compile it with the v flag, so a hyphen inside [ ] must be escaped as \- . For Invalid escape, remove backslashes that do not mean anything in u or v mode.

Why the pattern is invalid, and the fix

  1. 01

    User input or a filename used as a pattern

    “When dynamically creating a RegExp with user-provided content, consider using this function to sanitize the input (unless the input is actually intended to contain regex syntax).” MDN, RegExp.escape()

    new RegExp(search) breaks as soon as someone searches for +1 (555) or *.js, because + * ( and ? are regex syntax. Escape the text first. RegExp.escape is Baseline 2025, so keep a fallback if you support older browsers.

    const pattern = RegExp.escape
      ? RegExp.escape(search)
      : search.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
    const re = new RegExp(pattern, 'i');
  2. 02

    A pattern attribute that the v flag rejects

    “No regular expression is applied if the attribute value is absent, an empty string, or invalid.” MDN, HTML attribute: pattern

    pattern="[a-z0-9-_]+" was fine for years, but browsers now compile pattern with the v flag, where a hyphen inside a character class must be escaped. The browser logs the error and stops checking that field, so bad input gets through.

    <!-- before: invalid with the v flag -->
    <input name="username" pattern="[a-z0-9-_]+">
    
    <!-- after -->
    <input name="username" pattern="[a-z0-9_\-]+">
  3. 03

    An unneeded backslash in u or v mode

    “This severely limits the ability to add new escape sequences in the future, so in Unicode-aware mode, only recognized escape sequences are allowed. Do not add \ redundantly.” MDN, SyntaxError: invalid identity escape in regular expression

    Without a flag, \- or \_ just mean the character itself. With the u or v flag, every escape must mean something, so /\-/u throws Invalid escape. Drop the extra backslash, or escape only characters that need it.

    // throws: Invalid escape
    /\-/u;
    
    // works
    /-/u;
  4. 04

    A group that is not closed, or syntax from another language

    “Maybe you are using a feature that is not supported by the current JavaScript engine.” MDN, SyntaxError: invalid regexp group

    Unterminated group means a ( has no matching ). Invalid group means (? is followed by something JavaScript does not support, such as Perl or PCRE syntax copied from another tool. Safari before 16.4 rejects lookbehind (?<=...), so a pattern can work in Chrome and fail on an older iPhone.

Checked against the vendor’s docs on : MDN: RegExp.escape(); MDN: HTML attribute: pattern; MDN: SyntaxError: nothing to repeat; MDN: SyntaxError: invalid identity escape; MDN: SyntaxError: invalid regexp group.

Rather hand it over?

Fixed scope, fixed price, paid upfront. The work happens in writing; no call needed.

Related: “Invalid or unexpected token” · “Unexpected identifier” · “undefined is not an object”

Questions

More on this error

What does Nothing to repeat mean?

A quantifier such as *, + or ? comes first, or right after ( or |, so there is nothing for it to repeat. In *.js the * is at the start. Escape it (\*) if you meant the character, or put the thing to repeat before it.

Why does my form pattern now show an error in the console?

Browsers now compile the pattern attribute with the v flag instead of the u flag; the switch landed in 2023. The v flag needs (, ), [, {, }, /, - and | escaped inside a character class when they are literal characters, as well as ] and \, so some older patterns became invalid and are ignored.

Can you fix it for me?

If one bug is the whole problem, the App Fix Ticket covers it: a written diagnosis first, then the fix. If the app has more going on, the Launch Check is a written check of the whole app. Each issue found is rated critical, high, medium or low, with where it is and how it was confirmed. No issues worth fixing, no charge. If the Launch Check finds nothing rated medium or higher, Simon refunds the full $450. You keep the report either way. This applies to the first 10 Launch Checks bought after October 30, 2026.

A different error?

Describe it and get a written quote

Send the error and what you were doing. You get a written scope, a fixed price and a timeline; Simon replies by email, usually within one business day.