How to Fix "Nothing to repeat" Errors in JavaScript

Ferenc Almasi • 2022 July 16 • 📖 2 min read

The "Uncaught SyntaxError: Invalid regular expression: Nothing to repeat" error occurs in JavaScript when your regular expression is not valid. In order to fix the error, locate where the error is originating from, and then verify that the regular expression that you are using is valid.

Here are some examples of invalid regular expressions that can commonly occur and might be causing the error in your case too:

// ❌ Not properly escaping special characters
/.**/ -> /.*\*/

// ❌ Using double plus signs after a word boundary
/w++/ -> /w+/

// ❌ Starting a regex with a quantifier
/?[a-z]/ -> /[a-z]?/
/+[a-z]/ -> /[a-z]+/
Copied to clipboard!

As a general rule, "Nothing to repeat" errors occur in JavaScript, whenever you have an invalid regular expression in your code. If you would like to try to experiment with regex, I recommend using where matches will be highlighted according to your expression. If you are looking for more resources on Regex, check out one of the resources below.


Remove ads
Remove ads

📚 Get access to exclusive content

Want to get access to exclusive content? Support webtips with the price of a coffee to get access to tips, checklists, cheatsheets, and much more. ☕

Get access Support us
Remove ads
Remove ads
Remove ads
🎉 Thank you for subscribing to our newsletter. x