Guide · Updated 2026-07-23
Common Regex Mistakes (and How to Fix Them)
Greedy quantifiers, unescaped metacharacters, flag mismatches, and other patterns that break in production but look fine in isolation.
Greedy matching eats too much
Dot and star match as much as possible. A pattern that wraps quotes around a greedy star on a line with two quoted strings matches from the first quote to the last, swallowing the middle. Prefer lazy quantifiers or negated character classes instead.
HTML and log parsing with an unbounded greedy star across tags or timestamps fails silently. Anchor to delimiters: a date shape like four digits, dash, two digits, dash, two digits beats an open-ended match.
When greedy behavior is correct but slow, bound repetition so the engine cannot runaway-backtrack on megabyte inputs.
Forgetting to escape metacharacters
Dots, parentheses, brackets, plus, question mark, and backslash are special in regex. A literal dot in `example.com` must be `example\.com` or you match 'exampleXcom'.
User-supplied search terms must be escaped before insertion into a pattern. Otherwise a query for `a+b` becomes 'a one-or-more b'.
Windows paths and JSON strings double backslashes. In JavaScript string literals, `\\` is one backslash in the actual pattern — another common tester-vs-code mismatch.
Flag and engine mismatches
Testing with case-insensitive `i` but deploying without it rejects valid matches. Document required flags next to the pattern in source comments.
Line anchors behave differently with and without multiline mode. `^` matches start of string or start of line only when `m` is set.
Lookahead and lookbehind vary by language. A pattern copied from Perl tips may not run in your browser or Go service.
Using regex where parsing belongs
Nested structures (JSON, HTML, nested parentheses) are not reliably regex-solvable. Use a parser, then apply small regex only on leaf strings if needed.
Validate emails, URLs, and phone numbers with dedicated libraries or platform APIs. Regex can screen obvious garbage, not legal compliance.
When a pattern exceeds one readable line, split into named steps with comments or functions. Future maintainers will thank you more than they will praise clever brevity.
Ready to try it? Open Regex Tester