Regex Reference Guide

"Invalid Regular Expression": Common Causes and How to Fix Each One

A regular expression engine parses your pattern as its own tiny language, with its own grammar for brackets, quantifiers, and escape sequences. When that grammar is violated, the engine throws before your code ever runs a match — usually with a message that names the syntax problem but not the character that caused it.

This error is especially common when patterns are built dynamically (concatenated from user input or variables) or copied between languages that don't share identical regex dialects. Below are the four causes that account for the vast majority of "invalid regular expression" errors, with a fix for each.

Invalid — unescaped special character
/price: $5.00 (usd)/
Valid — parentheses and dot escaped
/price: \$5\.00 \(usd\)/
Invalid — unbalanced group
/(foo|bar/
Valid — closing parenthesis added
/(foo|bar)/

1What error message will I see?

The wording differs by language, but all of them are telling you the same thing: the pattern string you passed in isn't valid regex syntax for that engine.

EnvironmentError message
JavaScriptSyntaxError: Invalid regular expression: /.../: Unterminated group
Python (re module)re.error: missing ), unterminated subpattern at position N
Javajava.util.regex.PatternSyntaxException: Unclosed group
.NET / C#System.Text.RegularExpressions.RegexParseException: Not enough )'s

Notice that every engine names the structural problem (an unterminated group, a missing bracket) rather than pointing at a single bad character. That's because regex parsers read left-to-right and only realize something's wrong once they reach the end of the string still expecting a closing token.


2The four most common causes

a. Unescaped special characters

Twelve characters carry special meaning in regex: . * + ? ( ) [ ] { } | ^ $ \. If you want to match one of them literally — a dollar sign in a price, a dot in a domain name — it must be escaped with a backslash. This is by far the most frequent cause, especially when a pattern is built from user-supplied text that happens to contain one of these characters.

b. Unbalanced groups or brackets

Every opening ( needs a matching ), and every [ needs a matching ]. This is easy to lose track of in long patterns with several nested groups, particularly when alternation (|) is involved.

c. A quantifier with nothing to repeat

Quantifiers (* + ? {n,m}) must follow a character, group, or character class. A pattern like *abc or a doubled-up quantifier like a++ (outside of engines that support possessive quantifiers) has nothing valid for the quantifier to attach to.

d. An invalid or duplicate flag

This one is specific to JavaScript's RegExp constructor and literal syntax: passing a flag the engine doesn't recognize, or passing the same flag twice (e.g. /foo/gg), throws SyntaxError: Invalid flags or Invalid regular expression flags rather than a group/bracket error.


3Fixes for each cause

Wrong — unescaped brackets
// Matching literal [tag] text
const re = /[tag]/;
// This matches a SINGLE
// character: t, a, or g
Correct — brackets escaped
const re = /\[tag\]/;
// Matches the literal
// text "[tag]"
Wrong — pattern built from raw input
// userInput = "3.5 (kg)"
const re = new RegExp(userInput);
// Throws: Invalid regular
// expression
Correct — escape before compiling
function escapeRegex(s) {
  return s.replace(
    /[.*+?^${}()|[\]\\]/g, '\\$&'
  );
}
const re = new RegExp(escapeRegex(userInput));

That last pattern is worth keeping on hand: any time you're building a RegExp from a variable rather than a literal, escape the twelve special characters first — otherwise any special character in the input (a stray parenthesis, a plus sign in a phone number) can throw at runtime.


4How to find and fix it

Paste your pattern into our Regex Tester — it validates as you type, so you'll see immediately whether the expression compiles, plus a plain-English breakdown of what each token does. That makes it much faster to spot an unescaped character or a missing bracket than reading the raw pattern character by character.

Need a working pattern instead? Regex Cheat Sheet has ready-to-use patterns for email, URL, phone, IP, and hex color.

Test your pattern and catch syntax errors instantly.

Live validation, match highlighting, and a plain-English explanation — right in your browser.

Open the free Regex Tester