Home/Blog/Invalid Regular Expression: Nothing to Repeat ? Regex Fix Guide
??
javascriptregexregexp

Invalid Regular Expression: Nothing to Repeat ? Regex Fix Guide

The 'nothing to repeat' regex error happens when a quantifier like *, +, or ? has nothing to apply to. Learn the exact triggers, how to escape metacharacters, and how to write valid regex.

May 22, 20263 min readby ToolNinja

The Exact Error

SyntaxError: Invalid regular expression: /+test/: Nothing to repeat

Or:

SyntaxError: Invalid regular expression: /?pattern/: Nothing to repeat
SyntaxError: Invalid regular expression: /*/: Nothing to repeat

Quick summary: A quantifier (*, +, ?, {n}) was placed at the start of a pattern or after another quantifier. Quantifiers must follow a character, group, or character class ? they need something to apply to.


Why This Error Happens

Quantifiers are operators that specify "how many" of the preceding element to match. When there's no preceding element ? at the start of the pattern, after (, or after | ? the regex engine throws.

Common triggers:

1. Quantifier at start of pattern ? /+test/ or /*pattern/

2. Literal intended ? User wants to match a literal + but writes /+/ instead of /\+/

3. Double quantifier ? /a++/ ? a quantifier after another quantifier

4. User input in dynamic regex ? new RegExp(userInput) where user types *word*


Step-by-Step Diagnosis

Step 1 ? Find the position

try {
  const regex = new RegExp(pattern);
} catch (e) {
  console.error('Regex error:', e.message);
  // "Invalid regular expression: /+test/: Nothing to repeat"
}

Step 2 ? Check what's before the quantifier

const pattern = '+test';
// + is at position 0 ? nothing before it!

const pattern2 = 'a++';
// second + has only the first + before it ? also a quantifier

Step 3 ? Check for dynamic regex from user input

const search = req.query.q;  // User types "*word*"
const regex = new RegExp(search);  // Throws!

Solutions

Solution 1 ? Escape the metacharacter

const regex1 = /\*/;   // Matches literal *
const regex2 = /\+/;   // Matches literal +
const regex3 = /\?/;   // Matches literal ?

Solution 2 ? Add the element before the quantifier

// WRONG:
const bad = /+word/;

// RIGHT:
const good = /\w+word/;
const good2 = /.+word/;
const good3 = /[a-z]+/;

Solution 3 ? Escape user input in dynamic regex

function escapeRegex(string) {
  return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}

const regex = new RegExp(escapeRegex(userInput), 'gi');

Solution 4 ? Use try-catch for dynamic regex

function safeRegex(pattern, flags = '') {
  try {
    return new RegExp(pattern, flags);
  } catch (e) {
    console.error(`Invalid regex: "${pattern}"`, e.message);
    return null;
  }
}

Quick Reference ? Metacharacter Escape Cheat Sheet

CharacterMeaningEscaped literal
.Any character\.
*Zero or more\*
+One or more\+
?Zero or one\?
^Start of string\^
$End of string\$
( )Group\( \)

Prevent This Error in the Future

1. Always escape user input before using it in new RegExp().

2. Use regex literals (/pattern/) for static patterns ? your editor flags invalid literals at development time.

3. Test complex regex patterns in isolation against valid, edge-case, and failing test strings.


Use ToolNinja to Debug Faster

The Regex Tester shows the exact error position and highlights matching parts of input in real time. Fix the pattern and immediately see which test strings match.

?? Regex Tester ? toolninja.io/tools/regex-tester

Share:𝕏 Twitterin LinkedIn

Frequently Asked Questions

What does 'nothing to repeat' mean in a regex error?

Quantifiers like *, +, ?, and {n} modify the preceding element. If there is no preceding element ? because the quantifier is at the start of the pattern or after another quantifier ? the regex engine throws 'nothing to repeat'.

How do I use * or + as a literal character in a regex?

Escape them with a backslash: /\*/ matches a literal asterisk, /\+/ matches a literal plus sign. The metacharacters that must be escaped are: . * + ? ^ $ { } [ ] ( ) | \.

What is the difference between .* and .+ in regex?

.* means zero or more of any character. .+ means one or more of any character. The dot is the preceding element that * or + applies to. Writing just * or + without a preceding element is invalid.

Can I use regex special characters inside a character class []?

Inside a character class, most metacharacters lose their special meaning. [.*+?] matches a literal dot, asterisk, plus, or question mark. However, ] - ^ \ still have special meaning inside character classes.

🥷 ToolNinja