Developer tools

Regex Tester

Test a regular expression against sample text with live highlighting, match details, capture groups and replace.

Enter a pattern to begin
/ /g
Matches
Match details

    What it is for

    Regular expressions are compact patterns for finding and transforming text, and almost nobody writes one correctly on the first try. This tester runs your pattern against sample text as you type, highlights every match in place, lists each match with its position and capture groups, and shows the result of a replacement so you can check the whole thing before it goes into code. It uses the JavaScript regex engine, which shares most syntax with Python, Java, Go and PCRE: character classes, quantifiers, groups, alternation, lookahead and named groups all behave the same way.

    Regex cheat sheet

    Click any item to add it to the pattern.

    Characters

    .
    Any character except a line break
    \d
    A digit, 0 to 9
    \w
    A word character: letter, digit or _
    \s
    Whitespace: space, tab, line break
    \D \W \S
    The opposite of \d \w \s
    [abc]
    One of a, b or c
    [^abc]
    Any character except a, b or c
    [a-z0-9]
    One character in the ranges
    \.
    A literal dot (escape . * + ? ( ) [ ] { } | ^ $ \)

    Quantifiers

    *
    Zero or more
    +
    One or more
    ?
    Zero or one
    {3}
    Exactly 3
    {2,5}
    Between 2 and 5
    {2,}
    At least 2
    +?
    Lazy: as few as possible (also *? and ??)

    Anchors

    ^
    Start of text (of a line with the m flag)
    $
    End of text (of a line with the m flag)
    \b
    Word boundary

    Groups

    (abc)
    Capture group, available as $1
    (?:abc)
    Group without capturing
    (?<name>abc)
    Named group, available as $<name>
    a|b
    a or b
    \1
    Same text as group 1 matched
    (?=x)
    Followed by x (lookahead)
    (?!x)
    Not followed by x
    (?<=x)
    Preceded by x (lookbehind)

    Ready-made patterns

    [\w.+-]+@[\w-]+(?:\.[\w-]+)+
    Email address
    https?://[^\s/$.?#][^\s]*
    Web address
    \b(?:\d{1,3}\.){3}\d{1,3}\b
    IPv4 address
    (\d{4})-(\d{2})-(\d{2})
    Date as YYYY-MM-DD
    #(?:[0-9a-fA-F]{3}){1,2}\b
    Hex colour
    ^-?\d+(?:\.\d+)?$
    Whole number or decimal

    How to use it

    1. Type the pattern without the surrounding slashes. Turn on the flags you need: g finds every match rather than just the first, i ignores case, m makes ^ and $ match at line breaks, s lets . match newlines.
    2. Paste the text to search below. Matches are highlighted immediately, and the list underneath shows each match with its index and any capture groups.
    3. To try a replacement, fill in the Replace field. Use $1, $2 or $<name> to insert captured groups into the result.

    Questions

    Why does it only find the first match?
    The g (global) flag is off. Without it a regex stops at the first match, which is also how most programming languages behave by default.
    How do I match a literal dot, plus or bracket?
    Put a backslash in front of it: \. \+ \[ and so on. The characters . * + ? ( ) [ ] { } | ^ $ \ all have special meanings and need escaping to match themselves.
    Will my pattern work in Python or PHP?
    Usually. The core syntax is shared. The main differences are lookbehind support, how named groups are written ((?P<name>) in Python versus (?<name>) here) and a few shorthand classes. Test edge cases in the target language before shipping.