Quick answer
A valid regular expression must have balanced brackets, valid escape sequences, and correct quantifier placement.
Regex Invalid Pattern
A valid regular expression must have balanced brackets, valid escape sequences, and correct quantifier placement. Invalid syntax causes the engine to throw or fail.
Common causes
- Unclosed bracket or parenthesis (e.g. [a-z or (group).
- Invalid escape (e.g. \k without a named group, or \ followed by an invalid letter).
- Quantifier applied to nothing (e.g. * or + at the start, or | with nothing on one side).
How to fix
- Match every [ ( { with a closing ] ) }.
- Use valid escapes: \d \s \w \n \t or \ then a special character to match it literally.
- Ensure * + ? apply to a preceding token; use (?:...) or [...] for empty alternatives.
- Test with an online regex tester to see the exact error.
Examples
Bad
[a-z
Good
[a-z]+
FAQ
- What does 'invalid regular expression' mean?
- The pattern has a syntax error: unclosed group, bad escape, or invalid quantifier. Use a regex tester to get the exact message and position.
- Can I use \s and \d in JavaScript regex?
- Yes. \d is digits, \s is whitespace. Use a regex literal /\d+/ or new RegExp('\\d+').
Fix it now
Try in validator (prefill this example)