Regex Tester

Test your regular expressions in real-time with syntax highlighting and a cheatsheet.

/ /
Flags

Pattern explained

TokenMeaning
Replace

Result

        
Use it in code
JavaScript literal
As a string for new RegExp()
Cheatsheet
Characters
.Any character except a line break (with s: any)
\d \DA digit / not a digit
\w \WA word character (letter, digit, _) / not one
\s \SWhitespace / not whitespace
[abc] [a-z]One of the listed characters or ranges
[^abc]Any character not listed
\p{L} \p{Script=Greek}A character with a Unicode property (needs the u flag)
Quantifiers
* + ?0 or more / 1 or more / 0 or 1
{3} {3,} {3,5}Exactly 3 / 3 or more / 3 to 5
*? +? ??Lazy: as few as possible
Anchors and boundaries
^ $Start / end of the text (of each line with m)
\b \BA word boundary / not a word boundary
Groups and lookaround
(abc)Capturing group, referenced as $1 or \1
(?<name>abc)Named group, referenced as $<name> or \k<name>
(?:abc)Group without capture
a|ba or b
(?=abc) (?!abc)Lookahead: followed / not followed by abc
(?<=abc) (?<!abc)Lookbehind: preceded / not preceded by abc
Escapes
\. \* \( \\The character itself, not its special meaning
\n \t \rLine feed, tab, carriage return
\xHH \uHHHH \u{...}A character by its code (\u{...} needs the u flag)
Flags
gfind all matches, not only the first
iignore case
m^ and $ match at line breaks too
s. matches line breaks too
uUnicode mode (\u{...}, \p{...}, astral characters as one)
ysticky, the match must start exactly at the end of the previous one
dthe engine also records where each group matched (indices)

Rate this tool

How useful was this tool?

Regex Tester

Write a regular expression in Pattern, between the two slashes, tick the Flags you need, and paste your text into Test text. Matches are highlighted as you type, and what each group captured is underlined.

Below the text you get:

  • Matches: every match with its position, the matched text and each group by name or number.
  • Pattern explained: every part of your pattern in plain words.
  • Replace: preview a replacement on the whole text.
  • Use it in code: the pattern as a JavaScript literal and as a string for new RegExp(), each ready to copy.
  • Cheatsheet: the syntax at a glance.

Example loads a working pattern, and Clear starts over. A pattern that runs away is stopped after two seconds instead of freezing your browser.

Example: finding email addresses

The Example pattern finds email addresses and captures the two halves by name:

/(?<user>[\w.+-]+)@(?<domain>[\w-]+\.[a-z]{2,})/g

in this text:

Contact us at [email protected] or [email protected].
Old address: [email protected] (retired in 2024).

The result says 3 matches in 0 ms:

# Position Text Groups
1 14–33 [email protected] user: support, domain: example.com
2 37–54 [email protected] user: sales, domain: example.org
3 69–90 [email protected] user: webmaster, domain: example.net

Positions count characters from 0, and the end is just after the last character of the match. “[email protected]” starts at 14 because “Contact us at “ is 14 characters long.

Pattern explained

The explanation turns the pattern into plain words, token by token. For the example, part of it reads:

Token Meaning
(?<user> group 1, named “user”, starts: what it matches is captured
[\w.+-] one character from the set: a word character (a letter, a digit or _), “.”, “+” or “-”
+ the preceding item, 1 or more times, as many as possible
\. the literal character “.”
[a-z] one character from the set: a to z
{2,} the preceding item, 2 or more times, as many as possible

That’s the quickest way to understand a pattern you found in someone else’s code, or to check that yours says what you meant.

Improving the example pattern

The example works on its sample text, but try it on other addresses and you’ll find its limits:

A better version allows several dot-separated parts, with the i flag for case:

/(?<user>[\w.+-]+)@(?<domain>(?:[\w-]+\.)+[a-z]{2,})/gi

Now the first address is matched in full, with the domain mail.example.co.uk, and the capital letters match too. Testing a pattern against the awkward cases, not just the ones you had in mind, is what this tool is for.

One caution: no regular expression can tell whether an email address really exists. For that, see the E-Mail Validator, or send a confirmation email.

Flags

Flag What it does
g Find all matches, not only the first.
i Ignore upper and lower case.
m ^ and $ match at the start and end of every line, not just the whole text.
s . matches line breaks too.
u Unicode mode: \u{…} and \p{…} work, and emoji count as one character.
y Sticky: each match must start exactly where the previous one ended.
d The engine also records where each group matched (indices).

Replace

Type a replacement and see the whole text with it applied. In the replacement:

  • $1 or $<name> inserts what a group captured
  • $& inserts the whole match
  • $$ inserts a dollar sign

With g, every match is replaced; without it, only the first.

With the example pattern:

  • ***@$<domain> hides who the addresses belong to: “Contact us at ***@example.com or ***@example.org.”
  • $<user> at $<domain> writes them out so they’re harder to harvest: “support at example.com”.
  • An empty replacement removes every match: “Contact us at or .”

Use it in code

JavaScript literal gives the pattern as you’d write it in JavaScript:

/(?<user>[\w.+-]+)@(?<domain>[\w-]+\.[a-z]{2,})/g

As a string for new RegExp() gives the same pattern as a string. Every backslash is doubled, because a string uses \ for its own escapes:

"(?<user>[\\w.+-]+)@(?<domain>[\\w-]+\\.[a-z]{2,})"

Forgetting to double them is one of the most common reasons a pattern works here but not in code.

The tester uses your browser’s JavaScript engine. Most patterns work the same in other languages, but a few things differ:

  • Python writes named groups as (?P<name>…) and uses raw strings, r"…".
  • PHP wraps the pattern in delimiters, '/…/i'.
  • Java needs every backslash doubled in the string, like the new RegExp() version.

Runaway patterns

Some patterns take forever on the wrong text. In ^(a+)+$, the engine can split a long run of as between the inner and outer + in an enormous number of ways. When the text ends in something that doesn’t fit, such as aaaaaaaaaaaaaaaaaaaaaaaaaaaa!, it tries every one of them before giving up. Each extra character roughly doubles the work. This is called catastrophic backtracking.

In most testers, such a pattern freezes the browser tab. Here it’s stopped after two seconds, and you’re told so.

It’s worth fixing, not just avoiding. On a server, a pattern like this can be exploited to tie up the processor (a “ReDoS” attack). A single badly behaved regular expression in a firewall rule took a large part of Cloudflare’s network down for about half an hour in 2019. The usual fix is to remove nested repetition: ^(a+)+$ means the same as ^a+$, which is instant.

FAQ

Which kind of regular expressions does it use?

JavaScript’s, through your browser. That covers named groups, lookahead, lookbehind and Unicode properties. See “Use it in code” for how other languages differ.

Why doesn’t my pattern match?

The usual suspects:

  • A . that should be \.. A plain dot means any character.
  • A missing g flag, when you expected more than one match.
  • Upper and lower case, fixed with the i flag.
  • ^ and $ in multi-line text, which need the m flag to work per line.
  • A greedy .* that swallows more than you meant (see the next question).

What’s the difference between greedy and lazy?

Quantifiers like + and * take as much as they can. In <b>bold</b>, the pattern <.+> matches the whole thing. Add a ? to make them lazy, and <.+?> matches <b> and </b> separately.

Is my text sent anywhere?

No. Matching runs in your browser, and nothing you type is stored.

Note: All processing is done client-side. We do not store or see your data.

Contact

Missing something?

Feel free to request missing tools or give some feedback using our contact form.

Contact Us