webtools

Regex Tester

Private: nothing leaves your browser

Flags

Waiting for a pattern

Matches highlighted

                

    Write a pattern and see exactly what it matches, highlighted in your own text, with every capture group listed. Syntax errors are explained as you type, and you can try a replacement without leaving the page.

    About this regex tester

    A regular expression is easy to write and hard to be sure about. The useful thing is not the pattern, it is seeing what the pattern actually does to real text: which runs it matched, which it skipped, and what landed in each capture group. This runs your pattern on every keystroke with the browser's own engine, which is the same one your JavaScript will use.

    Because it is the native engine, what you see here is exactly what you get in JavaScript, including the details other testers gloss over: a pattern without the g flag stops at the first match, an unmatched optional group comes back as undefined rather than an empty string, and a pattern that can match nothing produces empty matches that are counted but have nothing to highlight.

    Two things catch almost everybody. The shorthand classes are narrower than they look: \d, \w and \b are ASCII only in JavaScript, so a Devanagari digit is not a \d, the accent in "café" is not a \w, and \bcafé\b therefore fails to match the word on its own. Matching real names and real numerals means \p{L} and \p{N}, which need the u flag ticked. The other one is state: a pattern with g on it remembers where it stopped, so in your own code calling .test() twice on the same string gives true and then false. It cannot happen here, because a fresh expression is compiled on every keystroke, which is worth knowing when the page says a pattern matches and your code says it does not.

    This page is for working a pattern out. Once you have one, running it over a whole document is a different job, and the find and replace tool takes the same syntax with $1 style group references in the replacement.

    How to test a regular expression

    1. Type the Pattern without the surrounding slashes. They are already there.
    2. Tick the Flags you need. g is on by default, so you see every match rather than only the first.
    3. Paste your text below. Matches are highlighted immediately.
    4. Read the list underneath for each match's position and capture groups.
    5. Type a Replace with value to preview a replacement. Use $1 and $2 for groups.

    Common questions about regular expressions

    Is the text I test a pattern against stored or logged?

    No. The pattern runs in JavaScript on this page, so log extracts, customer data and anything else you are testing against stay on your own device. Nothing is sent, logged or saved.

    Which flavour of regex is this?

    JavaScript, because it is your browser's own engine. It is close to PCRE for everyday patterns but not identical: there are no possessive quantifiers or recursion, and lookbehind needs a reasonably recent browser. Python, Java and Go each differ in their own small ways, so test there too if that is where the pattern will live.

    Why do I only see one match?

    The g flag is what makes a pattern keep going after its first hit. If it is switched off, one match is the correct answer and the status line says so. It is the single most common surprise when moving a pattern into code.

    What are named capture groups?

    Write (?<year>\d{4}) instead of (\d{4}) and the group gets a name, which is much easier to read six months later than $1. Named groups are listed here by their names, and you can use $<year> in the replacement.

    Why did my pattern make the page slow?

    Some patterns backtrack catastrophically: nested quantifiers such as (a+)+b can take exponential time on text that nearly matches. The test text here is capped to keep that survivable, but the real fix is the pattern. If it hangs on long input, it will hang in production too, which is a denial of service waiting to happen.

    Next

    Related tools

    All Dev tools