2026-08-07

Common regex mistakes and how to fix them

Fix missing global flag, greedy quantifiers, unescaped characters, and flavor mismatches. Test patterns privately.

Regex bugs are usually small and hard to spot. Below are mistakes that show up constantly when validating forms, parsing logs, or writing find-and-replace rules - plus how to catch them in a tester before they ship.

Forgetting the global flag

Without g, JavaScript RegExp returns only the first match. If your UI lists one hit when the string has many, turn global on.

Dot does not match newlines

By default . stops at newline. Multi-line blobs need the s (dotAll) flag, or an explicit character class that includes newlines.

Greedy quantifiers over-matching

Patterns like {.*} can swallow more text than you expect. Prefer tighter classes, non-greedy *?, or clearer delimiters. Always verify against a highlighted preview.

Unescaped special characters

Characters such as ., +, (, and $ are metacharacters. To match them literally, escape with a backslash (for example \. for a real period).

Wrong flavor assumptions

A pattern copied from a PCRE or Python answer may fail in JavaScript. Test in the engine you will run in production. Browser lookbehind and Unicode support vary - check the error message and simplify if needed.

How to debug quickly

  1. Open the regex tester.
  2. Paste the failing pattern and a minimal reproducing string.
  3. Toggle flags one at a time; inspect groups after each change.
  4. Narrow the pattern until the highlight matches only what you intend.

Debug regex mistakes free - live matches, flags, and groups in your browser.
Open the free regex tester →

Related: What is a regular expression? · How to test regex online