AXS TXT
← All articles

How to Test a Regular Expression (Without Guessing)

July 19, 2026 · Updated October 22, 2026 · Silviu Anca

A regular expression that looks right can match too much, too little, or something you never thought of. The reliable way to write one is to test it against real text after every change, rather than writing the whole pattern and hoping.

Testing as you type

  1. Open Regex Tester.
  2. Type the pattern between the slashes and the flags in the small box next to it.
  3. Paste some text below. The tester counts the matches and lists each one (up to 200) as you type. An empty match shows as ∅, which usually means part of your pattern is optional when it shouldn't be.

It uses the JavaScript regex engine of your browser — the same one your web front end will use — and the text never leaves the page.

The flags

FlagEffect
gfind every match, not just the first (the tester always adds it)
iignore upper and lower case
m^ and $ match at the start and end of every line, not only the whole text
sthe dot also matches line breaks
uUnicode mode: needed for \p{…} classes and to treat emoji as one character
ysticky: match only at the exact current position

Build it up in steps

Say you want the dates in "Due 14.03.2026, paid 2026-03-02".

  1. Start with the simplest thing that matches one case: \d{4}-\d{2}-\d{2} finds 2026-03-02.
  2. Add the other format as an alternative: \d{4}-\d{2}-\d{2}|\d{2}\.\d{2}\.\d{4}. Note the escaped dots — an unescaped dot means "any character", so 14x03x2026 would also match.
  3. Add word boundaries so you don't match inside longer numbers: \b(?:\d{4}-\d{2}-\d{2}|\d{2}\.\d{2}\.\d{4})\b.

Check the match list after each step. Then paste text that should not match — a phone number, an invoice number — and make sure the count stays where it should.

Mistakes that catch everyone

JavaScript is not PCRE

Patterns copied from a PHP, Python or grep example usually work, but not always. JavaScript supports lookbehind, named groups (?<name>…) and Unicode property classes, but not possessive quantifiers, atomic groups or recursive patterns. If a pattern from elsewhere reports "Invalid regular expression" here, one of those is the usual suspect.

When not to use a regex

To replace one fixed piece of text with another, Find and Replace is simpler and treats special characters literally. To pull values out of structured data, use the right parser: JSON Formatter for JSON, URL Parser for links, CSV to JSON for spreadsheets. A regex is the right tool when the pattern genuinely varies.

Try Regex Tester

Test a regular expression against your text and highlight every match — live, in your browser.

Open Regex Tester