toolqik

115 · Format & Test

Regex Tester

Live match highlighting, capture groups and replace preview.

Runs in your browser
/ / g
Test string

Matches

    Replace

    Result
    
      
    Cheat sheet
    .any character\d \w \sdigit, word, space\D \W \Snegated[abc] [^abc]set / not in seta* a+ a?0+, 1+, 0 or 1a{2,5}between 2 and 5a*? a+?lazy^ $start / end\bword boundary(abc)capture group(?<name>…)named group(?:…)non-capturinga|ba or b(?=…) (?!…)lookahead(?<=…) (?<!…)lookbehind$1 $<name>in replacement

    About this tool

    A regular expression (regex) is a small pattern language for finding, checking and replacing text: validating an email field, pulling IDs out of logs, or reformatting dates in bulk. This regex tester shows every match highlighted live as you type, lists the capture groups, and previews replacements. It uses your browser’s own JavaScript engine, so results match match, matchAll and replace exactly.

    How to use the regex tester

    1. Type a pattern between the slashes, without the slashes themselves.
    2. Toggle flags: g to find all matches, i to ignore case, m so ^ and $ work per line, and so on.
    3. Paste a test string. Matches are highlighted in place, and each one is listed with its position and capture groups.
    4. Optionally type a replacement to preview the result. Use $1 for numbered groups and $<name> for named ones.

    Patterns run in a background worker with a time limit, so a runaway pattern is stopped instead of freezing the page.

    Common regex patterns

    Click Try it to load a pattern, its flags and sample text into the tester above, then adapt it to your data.

    Pattern forRegex
    Email address
    Practical check, not full RFC 5322. Confirm real addresses by sending an email.
    ^[^\s@]+@[^\s@]+\.[^\s@]+$
    URL (http or https)
    For full validation in JavaScript, new URL(value) is more reliable.
    ^https?:\/\/[\w.-]+(?:\.[\w.-]+)+(?::\d+)?(?:[/?#]\S*)?$
    IPv4 address
    Rejects octets above 255 and leading zeros.
    ^(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)$
    UUID
    Versions 1–8, standard variant, any case.
    ^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$
    Date (YYYY-MM-DD)
    Checks the format and ranges, not the calendar: 2024-02-31 still matches.
    ^\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])$
    Time (24-hour HH:MM) ^(?:[01]\d|2[0-3]):[0-5]\d$
    Hex colour
    3, 4, 6 or 8 hex digits (with optional alpha).
    ^#(?:[0-9a-f]{3}|[0-9a-f]{4}|[0-9a-f]{6}|[0-9a-f]{8})$
    URL slug ^[a-z0-9]+(?:-[a-z0-9]+)*$
    Username
    3–16 letters, digits or underscores.
    ^[a-zA-Z0-9_]{3,16}$
    Strong password
    At least 12 characters with lower, upper, digit and symbol, using lookaheads.
    ^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^\w\s]).{12,}$
    Phone number (E.164)
    International format: +, country code, number, no spaces.
    ^\+[1-9]\d{6,14}$
    Indian mobile number
    10 digits starting with 6–9, with an optional +91 or 0 prefix.
    ^(?:\+91[\s-]?|0)?[6-9]\d{9}$
    Indian PIN code ^[1-9]\d{5}$
    Semantic version
    MAJOR.MINOR.PATCH with optional pre-release and build parts.
    ^(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-[0-9A-Za-z.-]+)?(?:\+[0-9A-Za-z.-]+)?$
    Repeated spaces
    Pair with a single space as the replacement to collapse them.
    {2,}

    For the email pattern, remember that no regex can tell you whether an address really exists: send a confirmation email. For URLs in JavaScript, new URL(value) is stricter than any short pattern.

    Regex flags

    FlagNameEffect
    gglobalFind all matches, not just the first
    iignore casea also matches A
    mmultiline^ and $ match at the start and end of every line
    sdotAll. also matches line breaks
    uunicodeFull Unicode, including emoji and \p{L} letter classes
    ystickyMatch only at the current position (lastIndex)

    Replace with capture groups

    Parentheses capture what they match, so you can reuse it in the replacement. To turn 2024-05-17 into 17/05/2024, use the pattern (\d{4})-(\d{2})-(\d{2}) and the replacement $3/$2/$1. With named groups, (?<year>\d{4}), the replacement becomes more readable: $<day>/$<month>/$<year>. $& inserts the whole match.

    Use regex in code

    const re = /(?<user>[\w.+-]+)@(?<domain>[\w-]+(?:\.[\w-]+)+)/g;
    
    re.test('ada@example.com');                       // true (careful: /g remembers lastIndex)
    'a@x.com b@y.org'.match(re);                      // ['a@x.com', 'b@y.org']
    
    for (const m of text.matchAll(re)) {
      console.log(m.index, m[0], m.groups.user, m.groups.domain);
    }
    
    '2024-05-17'.replace(/(\d{4})-(\d{2})-(\d{2})/, '$3/$2/$1'); // '17/05/2024'

    JavaScript regex vs. other languages

    Most syntax is shared between languages, but these differences catch people out when copying a pattern:

    • Python writes named groups as (?P<name>…), has no g flag (use findall or finditer), and only allows fixed-width lookbehind. JavaScript accepts variable-width lookbehind such as (?<=a+)b.
    • Go (RE2) has no lookahead, lookbehind or backreferences, in exchange for guaranteed linear-time matching.
    • PCRE, PHP and Java support possessive quantifiers (a++) and atomic groups ((?>…)), which JavaScript does not.
    • POSIX classes such as [[:alpha:]] don’t exist in JavaScript. Use [a-zA-Z] or, with the u flag, \p{L}.

    Catastrophic backtracking

    Nested or overlapping quantifiers, like (a+)+$ or (\w+\s?)*$, can make the engine try an exponential number of ways to match a string that almost matches. On a server this is a denial-of-service risk (ReDoS). This tester stops such patterns after 1.5 seconds. To fix one, remove the ambiguity: make the repeated parts unable to match the same characters, or anchor and simplify the pattern.

    FAQ

    How do I test a regular expression?

    Type the pattern in the box between the slashes and paste some text below it. Every match is highlighted as you type, with its capture groups listed underneath.

    What is the regex for an email address?

    A practical pattern is ^[^\s@]+@[^\s@]+\.[^\s@]+$, which checks for something@something.tld. Full RFC 5322 validation by regex is impractical; confirm addresses by sending an email.

    Which regex flavour is this?

    ECMAScript (JavaScript), as implemented by your browser, including named groups, lookbehind and the s/u/y flags. Most syntax is shared with PCRE, Python and Java, but some details differ.

    Why does my pattern only find one match?

    Turn on the g (global) flag to find all matches.

    What is catastrophic backtracking?

    Nested quantifiers such as (a+)+ can make the engine try an exponential number of paths on non-matching input. Rewrite the pattern to remove the ambiguity.

    esc
    ↑↓ navigate↵ open