How-to guide

How to Test a Regular Expression

To test a regular expression, enter the pattern and paste representative sample text. Matches are highlighted in place and capture groups listed separately. Work up from a pattern that matches too much, tightening it until it matches only what you want. Test against text that should not match as well — a regex that never rejects anything is not finished.

Regex Tester

Test a regular expression and see every match highlighted.

Open the tool

The mistake almost everyone makes is testing only against input that should match. A pattern that correctly finds every email address in your sample but also matches "not-an-email@" is worse than useless in production, and you will not discover that from happy-path testing.

The second mistake is nesting quantifiers. A pattern like (a+)+$ looks harmless and will hang a CPU for minutes on a modest input, because the engine explores exponentially many ways to split the string before giving up.

Step by step

  1. Paste realistic sample text

    Use real data, including the awkward cases: empty lines, unusual punctuation, accented characters, and strings that look close to a match but should be rejected.

  2. Start broad, then tighten

    Begin with a pattern that matches too much and narrow it. Starting from an over-specific pattern and loosening it tends to produce something that fits your sample and nothing else.

  3. Set the flags deliberately

    g finds every match rather than the first, i ignores case, m makes ^ and $ match at line boundaries rather than only at the ends of the string, s lets a dot match newlines.

  4. Read the capture groups

    Parentheses create a numbered group; named groups use (?<name>...). Use a non-capturing group (?:...) when you only need to apply a quantifier, so your group numbering stays stable.

  5. Prefer lazy quantifiers when scanning markup

    A greedy .* runs to the last possible match on the line. The lazy .*? stops at the first, which is nearly always what you want between two delimiters.

  6. Test the negative cases

    Add strings that must not match and confirm they do not. This is the step that catches the over-permissive pattern that would otherwise reach production.

Example

A lazy quantifier stops at the first closing bracket; a greedy one would run to the last.

Pattern and text

Pattern:  \[(.*?)\]
Flags:    g
Text:     [alpha] and [beta]

Matches

Match 1: [alpha]   group 1: alpha
Match 2: [beta]    group 1: beta

Frequently asked questions

Which regex flavour does this use?
JavaScript regular expressions, as implemented by your browser. That is close to PCRE for everyday patterns but differs in places: JavaScript has no atomic groups or possessive quantifiers, and lookbehind support depends on the browser version.
What is catastrophic backtracking?
A pattern with nested quantifiers, such as (a+)+$, can force the engine to try exponentially many ways of splitting the input before failing. On a few dozen characters that can take minutes. Avoid nesting a quantifier inside another quantified group.
Why does my pattern only find the first match?
The global flag is off. Without g, matching stops at the first result. Turn it on to scan the whole input.
Should I parse HTML with a regex?
Not for anything structural. HTML nesting is not regular, so a pattern that works on your sample will break on nested or malformed markup. Use a parser. A regex is fine for pulling a specific attribute out of known-shape markup.
Is my pattern or text sent anywhere?
No. Matching runs in your browser using its own regex engine, so both the pattern and the sample text stay on your device.

Tools used in this guide

All tools →
All guides →

Last reviewed .