Guides

How to Test a Regular Expression (Beginner's Guide)

Regular expressions (regex) are a compact language for matching patterns in text — validating emails, finding-and-replacing, parsing logs, extracting data. They look intimidating, but the core is small. This guide teaches the essentials and shows how to test patterns safely before you put them in code.

Follow along in the Regex Tester — type a pattern and some text and watch matches highlight live, in your browser.

Why test regex separately?

A regex that's slightly wrong fails silently — it matches too much, too little, or the wrong thing. Testing against real sample text before it goes into your code saves hours of debugging. A live tester shows you exactly what matches (and what doesn't) as you type.

The building blocks

Literal characters match themselves. cat matches "cat" anywhere in the text.

Character classes [ ] match any one character inside:

  • [aeiou] — any vowel
  • [0-9] — any digit (same as \d)
  • [a-zA-Z] — any letter
  • [^0-9] — any character that is not a digit (the ^ inside brackets negates)

Shorthand classes:

  • \d digit, \w word character (letters, digits, underscore), \s whitespace
  • \D, \W, \S are their negations
  • . matches (almost) any character

Anchors match positions, not characters:

  • ^ start of the string/line
  • $ end of the string/line
  • \b a word boundary

Quantifiers say how many:

  • * zero or more, + one or more, ? zero or one
  • {3} exactly 3, {2,4} between 2 and 4, {2,} 2 or more

Groups and alternation:

  • ( ) group part of a pattern (and capture it)
  • | means "or": cat|dog matches either

Flags change behavior:

  • g global (find all matches, not just the first)
  • i case-insensitive
  • m multiline (^/$ match each line)

Worked examples

  • A 5-digit zip code: ^\d{5}$ — start, exactly five digits, end.
  • A simple email-ish check: ^[^@\s]+@[^@\s]+\.[^@\s]+$ — something, an @, something, a dot, something (no spaces or extra @).
  • A hex color: ^#?[0-9a-fA-F]{6}$ — optional #, then six hex characters.
  • Find all words: \w+ with the g flag.
  • Trim trailing whitespace: match \s+$ (with m) and replace with nothing.

Paste any of these into the Regex Tester and try them against your own text.

Greedy vs lazy (the classic gotcha)

Quantifiers are greedy by default — they match as much as possible. On the text <a><b>, the pattern <.*> matches the whole <a><b>, not just <a>. Add ? to make a quantifier lazy: <.*?> stops at the first >, matching <a>. This single character fixes a huge share of "my regex matches too much" bugs.

Practical tips

  • Build incrementally. Start with the simplest pattern that matches one example, then expand. Test after each change.
  • Anchor when you mean "the whole thing." Validating a field? Use ^...$ so partial matches don't sneak through (\d{5} matches "abc12345"; ^\d{5}$ doesn't).
  • Escape special characters. To match a literal dot, use \.; same for * + ? ( ) [ ] { } ^ $ | \ /.
  • Beware catastrophic backtracking. Patterns like (a+)+ on certain inputs can hang. If a regex is slow on long text, simplify it.
  • Mind the dialect. JavaScript, PCRE, Python and others differ in edge features. Test in the environment you'll run in.

Don't use regex for everything

Regex is great for patterns in text, but it's the wrong tool for deeply nested structures. The classic example: don't parse HTML or JSON with regex — use a real parser. For JSON, see how to format and validate JSON.

Frequently asked questions

What does the g flag do? "Global" — it finds all matches instead of stopping at the first. Essential for find-all and replace-all.

Why does my pattern match more than expected? Usually greedy quantifiers. Try the lazy version (*?, +?), and anchor with ^/$ if you mean the entire string.

How do I match a literal special character? Escape it with a backslash: \. for a dot, \? for a question mark, and so on.

Is regex the same in every language? The basics are universal, but advanced features vary. Test in your target language — the Regex Tester uses JavaScript's engine.

Regex rewards a little practice. Open the Regex Tester, start small, and watch your matches light up as you build the pattern.

Related Articles