Skip to content
Guides

How to test a regex without copy-pasting into your editor

A regex tester is a debugger for patterns. Here is a workflow that produces a regex you can trust on the first paste.

May 26, 20264 min read
PATTERNSRegexGUIDEPATTERN[a-z]+abc 123 xyz2 matches

Writing a regex is the easy part. Writing a regex that does exactly what you meant and nothing else — that is where 90% of the time goes. A live tester is the cheapest way to close that gap.

The workflow that actually works

  1. Start with your simplest test case in the input — the string you definitely want to match.
  2. Write a pattern that matches it.
  3. Add a string you definitely don't want to match.
  4. Verify the pattern doesn't match it.
  5. Add a string that almost matches.
  6. Decide whether you want it to match or not. Adjust the pattern.
  7. Repeat until your test cases cover every edge case you can think of.

The Regex Tester updates highlights live, so each tweak gives instant feedback. The thing that wastes time in regex development is the round-trip from "edit pattern in code" to "run program" to "see whether it worked." Removing that round-trip is the entire game.

The flags that change everything

Six single-letter flags drive most regex behavior:

  • g (global): find every match, not just the first.
  • i (case-insensitive): a matches both a and A.
  • m (multiline): ^ and $ match at every line boundary, not just at start/end of the input.
  • s (single-line): . matches newlines too.
  • u (unicode): the pattern is interpreted as Unicode, so \w matches non-ASCII word characters and \u{...} syntax works.
  • y (sticky): matches only at lastIndex, used for tokenizers.

You almost always want g. You usually want u when handling user-typed text. The others are case-by-case.

Three patterns worth memorising

Most regex use cases reduce to one of:

Validation: does this string match a pattern? Use ^ and $ to anchor.

^[a-z0-9]+@[a-z0-9.-]+\.[a-z]{2,}$

Extraction: pull out parts of a string with capture groups.

^(\d{4})-(\d{2})-(\d{2})$

Then read match[1], match[2], match[3] for year, month, day.

Replacement: find-and-replace using capture groups.

"2026-05-27".replace(/^(\d{4})-(\d{2})-(\d{2})$/, "$3/$2/$1");
// "27/05/2026"

99% of regex work is one of these three. The Regex Tester here shows match positions and capture groups so you can verify before pasting into code.

What to be careful with

  • .* is greedy. It matches as much as possible. Use .*? for non-greedy ("as little as possible"). This single quirk causes more bugs than any other regex feature.
  • \d is not Unicode-aware unless you set the u flag. With u, \d matches Arabic, Devanagari, and other script digits.
  • Performance. Some patterns (especially nested quantifiers like (a+)+) backtrack catastrophically and hang on input. If your tester freezes on a string, that's the problem.
  • Don't parse HTML or JSON with regex. The tooling for parsing those properly is mature and free. Regex is for flat text.

For everyday flat-text matching — extract this date, find every URL, normalize this list — a few minutes in the regex tester saves an hour of "why doesn't this work."

Tags#regex#pattern#testing#how-to

More from Guides

See all →