Regex Tester: Test JavaScript Regular Expressions
Test a JavaScript regular expression against your text with the g, i, m, s, u, y and d flags. Highlights matches, lists numbered and named groups, previews replacements and explains bad patterns. Nothing is uploaded.
Type a JavaScript regular expression without the slashes, for example (\d{4})-(\d{2}).
Use $1, $<name>, $& and $$.
About the Regex Tester
The Regex Tester checks a JavaScript regular expression against a text while you type. Write the pattern without the surrounding slashes, tick the flags you need, and paste the text to search. Every match is highlighted in the text and listed with its position, its numbered groups and its named groups. Enter a replacement to preview what String.prototype.replace would return.
Nothing is uploaded; every match runs in your browser and never leaves your device.
How to use it
Flags
- g finds every match. Without it, only the first match is found, as
execdoes. - i ignores case, so
catalso matchesCatandCAT. - m makes
^and$match at the start and end of every line. - s lets
.match line breaks too. - u reads the pattern and the text as Unicode code points, so
.matches a whole emoji. - y is sticky: each match must start exactly where the last one ended, so
\dwith g and y on12a3stops at the letter. - d adds the start and end index of every group to the list.
Matches and groups
Positions in the match list count from 0, as JavaScript reports them, and the end is exclusive. A zero-length match, such as \b at a word boundary, is listed too; the search then moves on by one character, or one code point under u, so it never hangs. Up to 1,000 matches are listed and highlighted. Beyond that the summary says 1,000 or more matches.
Replace preview
The replacement supports $1 and the other numbered groups, $<name> for named groups, $& for the whole match and $$ for a dollar sign. With g every match is replaced; without it only the first.
Errors explained
The pattern is compiled on the page as you type, so a mistake shows at once next to the pattern field, with its position counted from 1. The common mistakes are explained in plain words: a group that is never closed, a ) with no group to close, a * or + with nothing to repeat, a character class without its ], and a range such as {2,1} whose minimum is larger than its maximum. Anything else shows the browser's own message.
Slow patterns are stopped
Some patterns take exponential time on a text that almost matches. The classic case is a nested repeat such as ^(a+)+$ on a long run of a followed by !. Matching therefore runs in a Web Worker, and the worker is stopped after 1 second, so the page always stays responsive. The page also warns, before you run anything, when a repeated group ends in + or * itself. Patterns are limited to 5,000 characters and texts to 1,000,000.
Related tools
Compare two versions of a text with the text diff tool, or tidy the JSON your pattern extracts with the JSON formatter.
Frequently asked questions
Which regex flavour does this tester use?
It uses the JavaScript RegExp engine of your own browser, the same one your web pages and Node.js code run. PCRE and Python features such as possessive quantifiers or the x flag are not available, and the v flag is not offered.
What do the g, i, m, s, u, y and d flags do?
g finds every match instead of the first, i ignores case, m makes ^ and $ match at each line, s lets the dot match line breaks, u reads the pattern and text as Unicode code points, y matches only where the last match ended, and d adds the start and end of every group.
Why does my pattern stop after one second?
Matching runs in a background worker that is stopped after 1 second, so a slow pattern never freezes the page. Nested repeats such as (a+)+ can take exponential time on a text that almost matches. Make the inner part unambiguous, for example a+ instead of (a+)+.
How do I use groups in the replacement?
Write $1, $2 and so on for numbered groups, $<name> for a named group, $& for the whole match and $$ for a dollar sign. With the g flag every match is replaced; without it only the first one is, exactly as String.prototype.replace does.
Why are positions counted from 0 in the list and from 1 in errors?
The match list shows the index that JavaScript itself reports, which starts at 0, so you can paste it into code. Error messages count from 1, like a text editor, so position 1 is the first character of the pattern you typed.
Is my text sent anywhere?
No. The pattern compiles and runs in your browser, in a worker on this device. The text you test, which may be a log file or customer data, is never uploaded or stored by this site.