"Invalid regular expression" Error, Explained
A SyntaxError: Invalid regular expression means the pattern itself doesn't parse — it's a compile-time failure, unrelated to whether it would match your input.
What triggers it
- Unbalanced groups or brackets. An unclosed
(,[, or{— e.g./(abc/or/[a-z/— is the single most common cause. - A dangling special character. A quantifier (
*,+,?) with nothing before it to repeat, like/*abc/, is invalid — the engine doesn't know what to apply it to. - An invalid escape sequence or empty group name in a named group like
(?<>...), or a backreference to a group that doesn't exist. - Copying a pattern from another regex flavor. PCRE, Python's
re, and JavaScript's regex engine diverge on some syntax (e.g. possessive quantifiers*+, atomic groups(?>...), and some Unicode property escapes are PCRE/Python-only) — a pattern that's valid in one is a syntax error in another.
How to debug it
Build the pattern up incrementally rather than pasting a large one in at once — comment out or delete the last group you added and re-test. Check that every opening bracket has a matching close, and that every quantifier follows something quantifiable.
The Regex Tester & Library reports JavaScript regex syntax errors immediately as you type, and includes a library of ready-made patterns (email, URL, UUID, etc.) if you're not sure your handwritten one is even necessary.