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!