Skip to content
Developer tools

Regex Tester

Test a JavaScript regex with live highlights, capture groups, replace and a plain-English reading of the pattern.

Last checked

Pattern

Examples

Test text

Matches

2 matches

in under 1 ms

Write to [email protected] or [email protected], not to ann@.

Match details

#PositionMatchGroups
19-24[email protected]none
228-55[email protected]none

Replace

Type a replacement to see the changed text.

What the pattern means

  1. [\w.+-]any one of \w.+-
  2. +one or more times
  3. @the character @
  4. [\w-]any one of \w-
  5. +one or more times
  6. (?:start of a group that does not capture
  7. \.the character .
  8. [\w-]any one of \w-
  9. +one or more times
  10. )end of the group
  11. +one or more times

Quick reference

.
Any character except a new line
\d \w \s
Digit, word character, whitespace
\D \W \S
The opposite of each
[abc] [^abc] [a-z]
A set, a negated set, a range
^ $
Start and end of the text (or line with m)
\b
A word boundary
* + ? {2,5}
Zero or more, one or more, optional, a count
*? +?
Lazy: as few as possible
( ) (?: ) (?<n> )
Capture, no capture, named capture
a|b
a or b
(?= ) (?! )
Lookahead, negative lookahead
(?<= ) (?<! )
Lookbehind, negative lookbehind
\1 \k<n>
Repeat a captured group
\p{L}
A letter in any script (needs u or v)

How to use it

  1. Type a pattern, or pick an example such as an email, a URL or an ISO date.
  2. Turn flags on and off, and paste your text. Matches are marked as you type.
  3. Read the match table and the plain-English reading, try a replacement, then copy the pattern as code or copy a link to share the test.

What it checks and why

This tester runs the regular expression engine of your own browser, so it follows JavaScript rules exactly: the result is what new RegExp gives in your code. The text you test never leaves the page. Patterns written for Python, PCRE, Java or .NET can differ, for example in lookbehind support, in the syntax for named groups and in what a dot matches, so check the flavour before you copy a pattern between languages.

Matches are found with a loop of exec calls. A pattern that can match nothing, such as a*, advances one character after an empty match (one whole code point with the u or v flag, as matchAll does), so the loop always ends. The tool stops at 1000 matches and at 200 000 characters of text to keep the page responsive. Each match shows its position, its text and each capture group, with the group name when it has one.

The most serious risk in regular expressions is catastrophic backtracking: a pattern such as (a+)+ takes exponentially longer on a long text that almost matches. In a page a bad pattern freezes the tab and on a server it blocks every request. This tester runs the pattern in a Web Worker and stops it after one second, so you see a Stopped message instead of a frozen page. It also flags the common shapes from the pattern alone; that is a warning, not a proof, so test with long input that does not match.

Specs and sources

Limits

  • It tests JavaScript regular expressions only. PCRE, Python, Java and .NET have different features and edge cases.
  • A run that takes more than one second is stopped and reported. A slow but finite pattern on a slow device can be stopped too; shorten the text to measure it.
  • The plain-English reading covers common syntax. Unusual constructs are shown as literal characters, so treat it as a guide and not as a parser.
  • The match table shows the first 100 matches, and matching stops at 1000. A shared link keeps up to 2000 characters of the test text.

Questions

How do I test a regex in JavaScript?
Write the pattern between slashes or in new RegExp, then call test, match, matchAll or exec on a string. This page does the same in your browser and shows every match, so you can adjust the pattern before you put it in code.
What does the g flag do in a regex?
It makes the pattern find every match in the text, not only the first. Without it, match returns the first result and replace changes only the first one, which is a common reason for a replace that seems to do nothing.
Why does my regex work here but not in Python or PHP?
Each language has its own regex engine. JavaScript lacks some features, such as atomic groups, and writes named groups as (?<name>...), where Python uses (?P<name>...). Check the syntax of the target language.
How do I match a literal dot or other special character?
Put a backslash before it: \. matches a dot, \? a question mark, \( a bracket. The characters that need it are . * + ? ^ $ { } ( ) | [ ] \ and, in a /literal/, the slash. Inside a class, [.] also matches a plain dot.
What is catastrophic backtracking in a regex?
A pattern like (a+)+ can try an exponential number of ways to split a long text that almost matches, and the engine does not give up. One bad pattern can freeze a browser tab or stall a server. Avoid nested quantifiers and test with long input.