Regex Tester

Type a regular expression and see every match highlighted as you type. Check capture groups, try a replacement, read what each part of the pattern does, and copy working code for JavaScript, Java or Python.

Test text
Matches
Explanation
    Code
    
          

    How to use the regex tester

    1. Type your pattern in the top box. You don't need the surrounding slashes.
    2. Tick the flags you need. g finds every match instead of stopping at the first one.
    3. Paste some sample text. Matches are highlighted straight away, and the table lists each match with its position and capture groups.
    4. Tick Replace to try a substitution, then copy the ready-made code for your language.

    Everything runs in your browser; your pattern and text are not uploaded. Your last session is kept in this browser, and Copy share link puts the whole test into a link you can send to a colleague.

    Regex cheat sheet

    SyntaxMeaning
    .Any character except a line break (with the s flag, line breaks too)
    \d \w \sA digit, a word character (letter, digit, underscore), a whitespace character. Capitals mean the opposite: \D is "not a digit".
    [abc] [^abc]One of a, b or c; any character except a, b or c. Ranges work too: [a-z0-9].
    ^ $Start and end of the text (of each line with the m flag)
    \bA word boundary: \bcat\b finds "cat" but not "catalog"
    * + ?Zero or more, one or more, zero or one of the previous item
    {3} {2,5} {2,}Exactly 3, between 2 and 5, 2 or more
    *? +?Lazy versions: match as little as possible
    (…) (?:…)A capturing group; a group that is not captured
    (?<name>…)A named group, used as $<name> in replacements
    a|ba or b
    (?=…) (?!…)Lookahead: followed by (or not followed by) something, without including it in the match
    (?<=…) (?<!…)Lookbehind: preceded by (or not preceded by) something
    \1Backreference: the same text that group 1 matched

    JavaScript, Java and Python differences

    The tester uses your browser's JavaScript engine. Most everyday patterns behave the same in Java and Python, but a few details differ:

    Why is my regex slow?

    Patterns with nested repetition, such as (a+)+$ or (.*,)*, can take exponentially long on text that almost matches. This is called catastrophic backtracking, and it's a known cause of outages and denial-of-service bugs. The tester stops any pattern that runs for more than 1.5 seconds and tells you. Fix it by making the repeated part more specific, for example [^,]*, instead of .*,.

    Frequently asked questions

    Which regex flavour does this tester use?

    It uses the JavaScript (ECMAScript) engine built into your browser, including lookbehind, named groups and Unicode property escapes. Everyday patterns work the same in Java, Python, PHP and .NET; the page lists the main differences, and the code panel adapts the syntax for Java and Python.

    How do I match across multiple lines?

    Use the m flag so ^ and $ match at the start and end of every line, and the s flag so . also matches line breaks.

    How do I use a capture group in the replacement?

    Write $1, $2 and so on for numbered groups, $<name> for a named group and $& for the whole match. For example, the pattern (\w+)@(\w+) with the replacement $2 at $1 turns "ann@example" into "example at ann".

    Is my text sent to a server?

    No. Matching runs in your browser in a background worker. The pattern and text are saved only in this browser so they are still there when you come back.

    Why does my pattern only find one match?

    Without the g (global) flag a regular expression stops at the first match. Tick g to find them all.