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
| Character | Meaning | Escaped 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.