You typed a pattern you were sure about, pasted your text, and got nothing. Or worse, a match on the wrong piece of text. A valid but wrong pattern fails silently: no error message, just an empty or surprising match list. Nearly every failure comes from a short list of causes. This guide works through them in order, using the free Regex Tester on Ovatly: for each, the failing pattern, the fix, and the match list evidence.

Contents

  • Quick answer
  • Read the match list first
  • Mistake 1: the missing g flag
  • Mistake 2: the unescaped dot
  • Mistake 3: the greedy quantifier
  • Mistake 4: missing anchors
  • Mistake 5: case sensitivity
  • Mistake 6: character class slips
  • Mistake 7: invalid pattern errors
  • A debugging routine, step by step
  • Worked example: fixing an email pattern
  • Limitations
  • Common mistakes
  • FAQs
  • Conclusion

Quick answer

Check in this order: the g flag (without it you see only the first match); escaped dots (\. for a literal dot); a greedy quantifier such as .* eating too much; anchors (^ and $) when the whole string must conform; case, or the i flag; and the class. If the tester reports an error instead of matches, look for an unclosed bracket, a trailing backslash, or a quantifier with nothing to repeat. Work the list in the Regex Tester, changing one thing at a time and re-reading the match list.

Read the match list first

Every fix here is proven the same way: by the match list. The tester runs a JavaScript regular expression against your text live and lists every match with its text, its index (where it starts) and its capture groups. Test on a short sample you fully understand, and change one thing at a time: rewrite three parts at once and you learn nothing.

Mistake 1: the missing g flag

Without the g flag, only the first match is reported, which matches how most code uses a pattern but looks like a bug when you expected a list. Run \d+ against the text Order 123 ships in 45 days. With no flags, the list shows one match: 123 at index 6. Add g in the Flags field and the same pattern reports two matches, 123 and 45. Fix: use g when you want every match; leave it out when one match is all you need.

Mistake 2: the unescaped dot

In a pattern, . means any character, not a full stop. So example.com matches the text ada@exampleXcom: the dot matches the X, and the match list duly shows exampleXcom. Not the domain you wanted. Fix: escape a literal dot as \.. The pattern example\.com against the same text reports no match, because now the pattern demands a real dot. This is the most common cause of false matches, above all in patterns for domains and file names.

Mistake 3: the greedy quantifier

Quantifiers such as * and + are greedy: they take as much text as they can while still allowing a match. Run <.*> against <b>bold</b> and <i>italic</i> and the list shows one giant match spanning the whole string. The pattern is valid; it just ate the middle. Fix: make the quantifier lazy with a question mark. <.*?> against the same text reports four matches: <b>, </b>, <i> and </i>. A stricter alternative is a negated class such as <[^>]*>, which cannot cross a closing bracket.

Mistake 4: missing anchors

A pattern matches anywhere inside a string unless you anchor it: right for searching, a bug when validating. The pattern \d{3}-\d{4} against the text call 555-12345 matches 555-1234, the first seven digits of a longer number, and reports success. A validator just accepted an eight-digit tail. Fix: anchor both ends when the whole string must conform. ^\d{3}-\d{4}$ against the same text reports no match: ^ demands the start of the string, $ the end, so nothing extra can hide on either side.

Mistake 5: case sensitivity

Matching is case sensitive by default. The pattern error against the text Error: disk full reports no match, because a capital E is not the lowercase character the pattern asks for. Log files and user input are full of this: the word is there, the case is not. Fix: add the i flag. With flags gi, the same pattern matches Error. Use it when case should not matter; leave it off when it should, as when lowercase and uppercase codes mean different things.

Mistake 6: character class slips

A character class matches one character from its set, and small slips change that set quietly. The classic is [A-z], written as shorthand for all letters. Run [A-z]+ against user_name! and it matches user_name, underscore included, because the range from Z to a covers six punctuation characters that sit between them. Fix: write both ranges out, [A-Za-z]. The reverse slip: [aeiou] matches a single vowel until you add a quantifier such as [aeiou]+. And a range written backwards, like [z-a], makes the pattern invalid, which leads to the last mistake.

Mistake 7: invalid pattern errors

Sometimes there is no match list because the pattern itself is broken: the tester reports a plain-language error instead of a stack trace, and the causes are a short list. An unclosed bracket or parenthesis, as in (\d+, leaves a group unfinished. A trailing backslash, as in \d+\, escapes nothing. A quantifier with nothing to repeat, as in +abc, has nothing it could repeat. Fix: read the error, count your opening and closing brackets, and check the end of the pattern for a stray backslash.

A debugging routine, step by step

  1. Open the Regex Tester and paste a short sample you can predict every match in.
  2. Type your pattern in the Pattern field, without slashes, and put any flags in the Flags field.
  3. Read the match list, and compare each match's text and index against what you expected.
  4. If the list is empty, work through the mistakes above in order: flags, dots, anchors, case, class. If it is too generous, suspect the greedy quantifier and missing anchors first.
  5. Change one thing, re-read the list, and move on only when the change explains itself; then paste the full text.

Worked example: fixing an email pattern

Start with the pattern (\w+)@(\w+).com against the text ada@exampleXcom. The match list shows a match, groups ada and example. That looks like success and is not: the address has no dot, and the unescaped . matched the X.

Apply mistake 2's fix: (\w+)@(\w+)\.com. Against ada@exampleXcom there is now no match. Against the genuine [email protected] it reports the match with groups ada and example, as in the tester's own example. One backslash turned a false positive into a correct test; most regex debugging is like this, a tiny fix that the match list confirms.

Limitations

Results follow JavaScript's RegExp rules, which differ slightly from PCRE, Python or grep, so a pattern fixed here may need adjustment elsewhere. Results stop at 500 matches: a run over a huge text shows the first 500, not a total count. A badly nested pattern can cause catastrophic backtracking on long text and keep your tab busy; simplify it and test on a short sample.

Common mistakes

  • Rewriting the whole pattern at once. Change one element, re-read the list, repeat; otherwise the culprit stays hidden.
  • Testing on the full document first. A wrong match hides in a long text; prove the pattern on a short sample instead.
  • Writing the pattern with slashes. The Pattern field takes the pattern body only; flags belong in the Flags field.
  • Treating a match as validation. A match inside a longer string does not prove the string conforms. Anchor when validating.

FAQs

Why does my pattern find the first match but not the rest? The g flag is missing. Without it, only the first match is reported. Add g in the Flags field when you want every match listed.

Why does my pattern match text it should not? The usual causes are an unescaped dot, which matches any character rather than a literal dot, a greedy quantifier that swallows too much, or missing anchors, which let the pattern match part of a longer string. The match list shows exactly what was matched; compare it with what you wanted.

Why does the tester say my pattern is invalid? Common causes are an unclosed bracket or parenthesis, a trailing backslash, or a quantifier with nothing to repeat. The tester shows a plain-language error, so read it and check those three first.

Why does my pattern work in another tool but not here? The tester uses JavaScript's RegExp engine, whose syntax differs slightly from PCRE, Python or grep flavors. A borrowed pattern may need small adjustments to behave the same way here.

Can a pattern make the tester hang? A pattern with nested quantifiers on long text can cause catastrophic backtracking and keep your browser tab busy. Simplify the pattern, and test on a short sample before pasting a long text.

Conclusion

A regex that does not match is rarely broken in a complicated way: it is missing a flag, trusting a dot, eating too much, floating unanchored, or tripping over case, and each has a one-line fix. When you are ready to build a pattern from scratch rather than repair one, the companion guide on matching dates with regex shows the piece-by-piece method. More guides like this one live in the Developer Tools guides archive.