Skip to content
DeviceCheckHub

Regex Tester

Try a regular expression on your own text. Every match is highlighted, groups are listed, and each part of the pattern is explained in plain words.

Type a pattern and some text to test it on. Everything runs in your browser.

Matches in the text

The matches are highlighted here.

Match details

    What the pattern means

    Type a pattern to see what each part of it means.

    Use this pattern in code

    Type a pattern to get code.

    Examples to start from

    How to test a regular expression

    1. Type your pattern between the slashes. The explanation below the text updates as you type.
    2. Paste the text to search into Test text. Every match is highlighted, in two alternating colors so neighbors can be told apart.
    3. Tick the flags you need. Keep g on to find all matches, and add i to ignore case.
    4. Open Match details to see where each match is, and the text of each capture group.
    5. To change the text, type a replacement and copy the result. Use $1, $2 or $<name> for groups.

    What a regular expression is

    A regular expression, or regex, is a short pattern that describes a set of strings. Instead of searching for one fixed word, you can search for things like “a date written as four digits, a dash, two digits, a dash and two digits”, which is the pattern \d{4}-\d{2}-\d{2}. Programmers use regular expressions to find text, check that input has the right shape, pull values out of logs and files, and rewrite text in bulk. Editors, command-line tools and every major programming language support them.

    The syntax you need most

    Flags

    Flags change how the whole pattern behaves. g finds all matches, i ignores case, m lets ^ and $ match at every line, and s lets the dot match line breaks. u switches on full Unicode support, which you need for emoji, for characters outside the basic range and for properties such as \p{L} (any letter in any language). v is an upgraded Unicode mode and cannot be combined with u, and y is the sticky flag that only matches at the exact position where the search starts.

    Common patterns, and their limits

    The example buttons load working patterns for email addresses, web addresses, dates, IPv4 addresses, hex colors and repeated words. They are good starting points, but treat them as rough filters rather than complete validators. The email pattern accepts most real addresses and rejects obvious mistakes, but the full rules for email are far more complicated, and the only sure test of an address is to send a message to it. The date pattern checks that the month is 01 to 12 and the day 01 to 31 but not that the day exists in that month. When something must be exactly right, use a purpose-built parser, such as a date library or the URL class, and keep the regex for the rough shape.

    Slow patterns

    Most regular expression engines, including the one in JavaScript, work by trying one way of matching and going back to try another when it fails. Usually that is fast. But a pattern with nested repeats, such as (a+)+$, can have an astronomical number of ways to split the text, and on text that nearly matches, the engine tries them all. Adding one more character can double the time. This is called catastrophic backtracking, and in a real program it can freeze a page or a server, which attackers have used to bring down websites. This tester runs the pattern in a background worker and stops it after two seconds, so you can find such a pattern safely. To fix one, avoid repeating a group that can match the same text in more than one way, make the parts more specific (for example [a-z]{1,20} in place of .*), and limit the length of the text you test.

    Use your pattern in code

    The Use this pattern in code box above writes the code for you, with the pattern, flags and replacement already turned into each language’s syntax and your test text (up to 300 characters) set as text, and notes where a language reads the pattern differently. For example, Python writes named groups as (?P<name>…), Go can’t run look-arounds or back-references, and in Python and .NET \d also matches digits from other scripts. By hand, the basics are:

    Tips

    Related tools: compare two versions of a text with the text compare tool, format data with the JSON formatter, or change letter case with the case converter.

    Frequently asked questions

    Which regular expression flavor does this tester use?

    It uses the JavaScript engine of your own browser, the same one that runs regular expressions in web pages and Node.js. That makes it exact for JavaScript and TypeScript. Most everyday patterns work the same in Python, PHP, Java, C# and Ruby, but some features differ, for example named groups are written (?<name>…) in JavaScript and (?P<name>…) in Python, and JavaScript has no \A, \Z, atomic groups or possessive quantifiers.

    Is my text or pattern uploaded?

    No. The pattern runs in your browser, in a background worker, and nothing is sent anywhere. The page remembers your last pattern, flags and text on this device so they are there when you come back, and Clear removes them.

    How do I share a regex with someone?

    Press Copy a link to this pattern. The link holds the pattern, flags and replacement, and the test text too if you tick that box, after a # sign. Browsers don’t send that part of a link to the web server when it is opened, but anyone you give the link to can read what it holds, so leave out text that is private.

    Why was my pattern stopped after 2 seconds?

    Some patterns can take an enormous amount of time on certain text. This is called catastrophic backtracking, and it happens when repeats are nested, such as (a+)+$, so the engine has to try exponentially many ways to split the text before it can say no. The tester runs your pattern in a worker and ends it after 2 seconds so that the page never freezes. In your own program such a pattern could freeze it or be used to attack it, so it is worth fixing.

    What do the flags g, i, m, s, u, v and y do?

    g finds every match instead of only the first. i ignores upper and lower case. m makes ^ and $ match at the start and end of each line. s lets the dot match line breaks. u turns on full Unicode handling, for example for emoji and \p{…} properties. v is a newer, stricter Unicode mode with set operations in character classes, and cannot be combined with u. y makes the match start exactly where the previous one ended.

    How do I replace text with the groups I captured?

    Type a replacement under the test text. In it, $1, $2 and so on stand for the first, second and later capture groups, $<name> stands for a named group, $& for the whole match and $$ for a dollar sign. For example the pattern (\w+) (\w+) with the replacement $2 $1 swaps two words. The result appears below, ready to copy.

    Can I use a pattern from here in another language?

    Yes. Under Use this pattern in code, pick JavaScript, Python, PHP, Java, C# or Go to get code with the pattern, flags, replacement and a short test text already converted, string escaping included, ready to paste, plus notes on what that language reads differently, such as Python’s (?P<name>…) groups or Go’s lack of look-arounds. Test the final code in your project before relying on it.

    Last updated · About DeviceCheckHub