Home / Code & Data / Regex Tester
04.06Code & Data

Regex Tester & Explainer

A live regex tester for JavaScript. Type a pattern, watch every match and capture group light up, try a replacement, and read what the expression actually does in plain English.

Updated
Flags

Matches highlighted

Match details

What this pattern means

    Common patterns

    JavaScript regex cheat sheet

    Characters

    .
    Any character except a line break
    \d \D
    Digit / not a digit
    \w \W
    Word character [A-Za-z0-9_] / not
    \s \S
    Whitespace / not whitespace
    [abc]
    One of a, b or c
    [^abc]
    Anything except a, b or c
    [a-z]
    One character in the range
    \. \* \\
    Escaped literal characters

    Anchors

    ^ $
    Start / end of string (of line with m)
    \b \B
    Word boundary / not a boundary

    Quantifiers

    * + ?
    0 or more, 1 or more, 0 or 1
    {3} {3,} {3,6}
    Exactly 3, 3 or more, 3 to 6
    *? +? {2,5}?
    Lazy: as few as possible

    Groups and lookaround

    (abc)
    Capturing group, $1 in replace
    (?<id>abc)
    Named group, $<id>
    (?:abc)
    Group without capturing
    a|b
    a or b
    \1 \k<id>
    Backreference to a group
    (?=x) (?!x)
    Followed by x / not followed by x
    (?<=x) (?<!x)
    Preceded by x / not preceded by x

    Replacement tokens

    $1 $2
    Numbered group
    $<name>
    Named group
    $&
    The whole match
    $` $'
    Text before / after the match
    $$
    A literal dollar sign

    A regex tester that explains itself

    A regex tester is the fastest way to stop guessing. Regular expressions are compact enough that one misplaced character changes everything, and reading someone else's pattern cold is hard even when you write them every week. This tool runs your pattern with the same JavaScript engine your code will use, highlights every match in the test string as you type, lists each capture group, previews a replacement, and breaks the pattern down token by token so you can see why it matches what it matches.

    How to use the regex tester

    1. Type the pattern between the slashes. Do not add the slashes yourself; they are shown only to make it look like a JavaScript literal.
    2. Set the flags. g finds every match instead of just the first, i ignores case, m makes ^ and $ work per line, s lets . cross line breaks, u turns on full Unicode handling and y anchors each match to where the last one ended.
    3. Paste real test data into the test string box. Include lines that should not match, because a pattern that matches too much is the more common bug.
    4. Check the match table. Each row shows the position of the match and the value of every group, numbered and named.
    5. Try a replacement. Fill in the replace box to see the output of String.replace with your pattern and flags.
    6. Read the explanation to confirm the pattern says what you meant, then copy it as a /regex/flags literal.

    Not sure where to start? The common patterns row loads ready-made expressions for emails, URLs, phone numbers, ZIP codes, hex colors, IPv4 addresses, ISO dates, URL slugs and strong passwords, each with sample text.

    Using the result in JavaScript

    Named groups make code far easier to read than m[3]. Here is the date pattern from the library in a real script:

    const re = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/g;
    const text = 'Shipped 2026-10-02, delivered 2026-10-05';
    
    for (const m of text.matchAll(re)) {
      console.log(m.index, m.groups.year, m.groups.month);
    }
    
    // Reformat every date as MM/DD/YYYY
    text.replace(re, '$<month>/$<day>/$<year>');
    // "Shipped 10/02/2026, delivered 10/05/2026"

    When the pattern comes from a string, every backslash has to be doubled, because the string literal consumes one of them first:

    new RegExp('\\d{3}-\\d{4}', 'g');   // same as /\d{3}-\d{4}/g
    new RegExp('\d{3}', 'g');           // wrong: becomes /d{3}/

    If you are building a pattern from user input, escape it first so characters like . and ? are treated literally. And if the text you are matching is URL-encoded, decode it with the URL encoder before testing so %20 does not hide the spaces your pattern expects.

    Common regex mistakes

    Greedy quantifiers swallowing too much

    <.+> on <b>bold</b> matches the entire string, not each tag, because .+ grabs as much as it can and then backs off only as far as it must. Use the lazy <.+?>, or better, a negated class like <[^>]+>, which is both clearer and faster.

    Forgetting the anchors when validating

    \d{5} finds five digits inside abc123456. For validation you almost always want ^\d{5}$ so the whole value has to match. With the m flag those anchors apply per line, which is handy for testing a list of values at once.

    Reusing a global regex with test()

    A regex with g or y keeps a lastIndex between calls. Calling re.test() on several strings in a row can return false for a valid value because the search started halfway through. Drop the g for one-off checks or reset lastIndex to 0.

    Catastrophic backtracking

    Nested quantifiers like (a+)+$ or (\w+\s?)*$ can take exponential time on input that almost matches. That is a real denial-of-service risk on a server that runs user-supplied text through a pattern. This tester runs the regex in a background worker and stops it after 1.5 seconds, with a warning, so you find the problem here instead of in production. The fix is usually to make the alternatives mutually exclusive or to remove one level of repetition.

    JavaScript flavor notes

    Modern JavaScript supports lookbehind, named groups, Unicode property escapes such as \p{L} (with the u flag) and, in current browsers, inline modifier groups like (?i:abc). It does not support possessive quantifiers, atomic groups, recursion, or the \A and \Z anchors from PCRE. A pattern copied from a PHP or Python answer usually works, but check those features first. Validating passwords? Pair a pattern with real entropy from the password generator rather than relying on complexity rules alone, and for structured data like API responses, parse it with the JSON formatter instead of regex.

    Frequently asked questions

    Which regex flavor does this tester use?

    JavaScript (ECMAScript), exactly as your browser runs it. Patterns that work here work in Node.js, Deno and front-end code. Most simple patterns also work in PCRE, Python and Java, but features such as possessive quantifiers, atomic groups and \A or \Z anchors are not available in JavaScript.

    Why does my regex only find the first match?

    The g (global) flag is off. Without it, exec, match and replace stop after the first match. Turn on g to find or replace every occurrence.

    How do I use a capture group in the replacement?

    Use $1, $2 and so on for numbered groups, $<name> for named groups, $& for the whole match and $$ for a literal dollar sign. For example, replacing (\d{4})-(\d{2})-(\d{2}) with $2/$3/$1 turns 2026-10-10 into 10/10/2026.

    What is catastrophic backtracking?

    It happens when a pattern can match the same text in an exponential number of ways, usually because of nested quantifiers like (a+)+ or overlapping alternatives like (\w|\d)*. On input that almost matches, the engine tries every combination and can freeze for seconds or minutes. This tester runs your pattern in a background worker and stops it after 1.5 seconds so the page stays responsive.

    Is the text I test sent anywhere?

    No. Matching, replacing and the explanation all run in your browser. Nothing you type is uploaded or stored.