Skip to content
TabShedKitsMy desk

Debug a Regular Expression Step by Step: Flags, Groups, Replacements and Slow Patterns

Updated 5 min readBy

A method for building and fixing a JavaScript regex in the Regex Tester: start small, read the match list, use groups and flags, and avoid catastrophic backtracking.

Regex TesterTest regular expressions live
Open Regex Tester
On this page
  1. What the tester is
  2. A method that works
  3. Flags
  4. Greedy matching
  5. Capture groups and replacements
  6. Start from a common pattern
  7. When the page says the pattern is too slow
  8. Other limits
  9. Common mistakes
  10. Related tools
  11. Frequently asked questions

A regular expression is easy to write and hard to debug. You type something that looks right, it matches too much, or nothing, and you cannot see why. The way out is to stop guessing and work in small steps while looking at what the pattern actually matches. This guide shows a method for doing that in the Regex Tester, and explains the three things that cause most trouble: flags, greedy matching and slow patterns.

What the tester is

The engine is JavaScript's own (ECMAScript) regular expressions, the same ones your browser and Node.js use, running in a background worker in your browser. Nothing is uploaded. If you write patterns for another language, such as Python, PCRE or Java, most basics are the same, but details such as lookbehind support, named groups and flags differ, so retest in the real environment.

A method that works

  1. Paste realistic test text first. Put in the Test string box real lines, including the ones that should match and, just as important, lines that should not. A pattern tested on only good examples will pass and fail later.
  2. Start with the simplest piece. Type a literal part of what you want, for example the word "invoice". Matches are highlighted as you type. Add one element at a time and look at the highlight after each.
  3. Read the Match details card. It lists each match with its position and the captured groups as $1, $2 and so on. If the count is not what you expect, you know the last change is the reason.
  4. Add anchors and boundaries when it matches too much: ^ and $ for the start and end of a line (with the multiline flag), \b for a word boundary.
  5. Only then add the replacement if you are rewriting text, and check the Replaced text box.

Flags

The flags are switches under the pattern: Global (g), Ignore case (i), Multiline (m) and Dot all (s); the Flags field also accepts u and y.

FlagEffectSymptom when missing
gFinds all matches, not just the firstOnly one match is highlighted, and a replacement changes only the first
iIgnores upper and lower case"Error" matches but "error" does not
m^ and $ match at each line, not just the whole textAn anchored pattern matches only the first line
sThe dot also matches a line breakA pattern with .* stops at the end of a line
uUnicode mode: handles characters outside the basic range correctlyEmoji and some letters are split into pieces
yMatches only from a fixed position (sticky)Rarely needed

"Why does it match only once?" is almost always a missing g flag. "Why doesn't ^ work?" is almost always a missing m flag.

Greedy matching

Quantifiers such as * and + take as much as they can. For text like <b>one</b> and <b>two</b>, the pattern <b>.*</b> matches the whole line from the first tag to the last, not each pair. Make it lazy with .*?, or better, say what you mean: <b>[^<]*</b> matches anything except a "<". A negated character class is clearer and faster than a lazy dot.

Capture groups and replacements

Parentheses capture part of a match. In the Replacement box use $1, $2 and so on to put captured text back. For example, to turn 2026-11-19 into 19/11/2026, match (\d{4})-(\d{2})-(\d{2}) and replace with $3/$2/$1. Groups that did not take part in a match show as empty. Use a non-capturing group (?:...) when you need grouping but not a number.

Start from a common pattern

The tester has a list of starting points: Email, URL, Phone (US), IP address, Date (YYYY-MM-DD), Hex color, Credit card and HTML tag. They are good for learning and for rough extraction, but not for strict validation. An email pattern simple enough to read will accept some invalid addresses and reject some valid ones; the IP pattern will accept 999.999.999.999. For a form, validate in code with a proper library or confirm the address by sending a message. Never use an HTML-tag pattern to parse real HTML.

When the page says the pattern is too slow

If a pattern takes more than one second, matching is stopped and you see "Pattern too slow". This protects your tab from catastrophic backtracking: patterns with nested repetition, such as (a+)+ or (.*)*x, can try an exponential number of ways to match a text that almost matches. The usual fixes:

  • Remove the nested quantifier; usually a+ alone does the job.
  • Make alternatives not overlap: (a|a)+ is bad, a+ is better.
  • Replace .* with a specific character class or a negated class.
  • Anchor the pattern so it fails early on text that cannot match.

A slow pattern in a server is also a security problem: user input that triggers backtracking can freeze the process. Test with a long almost-matching text before you ship a pattern.

Other limits

If a text produces more than 10,000 matches, the list shows the first 10,000 and says so. An invalid pattern, such as an unclosed bracket, is reported as an error rather than silently showing nothing.

Common mistakes

  • Forgetting to escape a dot. In a pattern, . means any character; to match a full stop write \. instead.
  • Double escaping. In a code string, backslashes must be written twice ("\\d" in a string literal) but in the tester you type the pattern itself: \d.
  • Testing on one line. Many patterns behave differently across lines; paste several.
  • Trusting a match on test data only. Add the hostile cases: empty input, extra spaces, long input, other languages.

To check how a text changed after a replacement, compare before and after in the Text Diff Checker. For patterns in URLs, see the URL Encoder and Decoder.

Frequently asked questions

Which regex flavour is it?

JavaScript (ECMAScript).

Is my text uploaded?

No. Matching runs in your browser, in a worker.

Why does my replacement do nothing?

The Replaced text appears only when the Replacement box is not empty, and without the g flag only the first match is replaced.