Regex Tester for JavaScript Patterns

Paste a pattern and some text. Every match is highlighted as you type, with its position, its capture groups and the flags in effect — and a clear error when the pattern does not compile.

Regex tester

Matched in this tab — nothing sent anywhere

Pattern Without the slashes
Text Highlighted live
Highlighted

      
Matches Index, text and groups

This is the JavaScript flavor. A pattern that works in Python, PHP or Go may not work here, and the differences are listed below.

What a match actually contains

A match is not just the text that matched. Every result carries the substring, its start index, and one entry per capturing group. A pattern like (\d{4})-(\d{2})-(\d{2}) against 2026-11-23 produces four entries: the whole date at index 0, then 2026, 11 and 23. Named groups — (?<year>\d{4}) — put the same text under a key, which is the difference between reading m[2] and reading m.groups.month six months later.

Two behaviors surprise people who have used regex in another language. The first is that in JavaScript test() on a pattern with the g flag is stateful: it remembers the last match position in lastIndex and starts the next search from there. Call it in a loop without resetting and it alternates true and false on the same string. The second is that exec() without g returns only the first match, so a page that shows one result is not lying — the flag is what asks for all of them.

The same pattern is not the same in every language

The regular expression engines people actually use fall into two families: backtracking engines (JavaScript, Python, PCRE in PHP, Ruby, Java) and linear-time engines (RE2 in Go, Rust's regex crate, and anything built on RE2). They accept overlapping syntax but not identical syntax, and the gaps are almost always in the same few features.

FeatureJavaScriptPythonPCRE / PHPRE2 / Go
LookbehindYes, since ES2018Yes, fixed width onlyYesNo
Backreference \1YesYesYesNo
Atomic group (?>)NoYes, 3.11 and laterYesNo
Named group syntax(?<name>)(?P<name>)(?<name>)(?P<name>)

So a pattern copied from a Python codebase can fail here for a bad reason: (?P<year>...) is a syntax error in JavaScript, and the fix is to drop the P. Conversely, Go's RE2 will refuse lookarounds and backreferences outright, which is a deliberate trade — those are exactly the features that make an engine's running time unpredictable.

Advertisement

Greedy is the default, and that is why .* ate your line

.* matches as much as it can while still letting the rest of the pattern succeed. Against <b>1</b> <b>2</b>, the pattern <b>.*</b> matches the whole string, because that is a valid match and it is the longest one. Adding ? makes the quantifier lazy and changes the answer to two separate matches: <b>.*?</b>.

The other direction — a quantifier that takes more than you wanted because it is not what you meant — comes from a character class that is too wide. [^,] stops at a comma; . does not, and it will also refuse to cross a newline unless the s flag is set. Most "the regex matched too much" bugs are one of those two.

Advertisement

Catastrophic backtracking, in one example

Nested quantifiers are the danger. Take (a+)+$ and feed it a long run of a followed by one character that is not a. The engine has to try every way of splitting that run between the inner and outer +, and the number of ways grows exponentially with the length. Twenty characters is instant; forty can take minutes. Nothing about the text looks hostile, and the pattern is short enough to pass review.

A linear-time engine like RE2 cannot have this problem, because it does not backtrack. A backtracking engine can be made safer by flattening the nesting — (a+)+ and a+ accept the same strings, so the extra group buys nothing — and by bounding the input. Anything that accepts a pattern from a user needs a limit on both the pattern and the text.

The u flag changes what a character is

Without u, JavaScript treats the input as UTF-16 code units. That is fine for ASCII and wrong for everything else: . matches half of an emoji, \d matches only the digits 0–9 and not Arabic-Indic ones, and \b only knows word characters that exist in ASCII. With u, . consumes a whole code point and \p{L} becomes available for "any letter" in any script.

The catch is that u is stricter about syntax. An unescaped - inside a character class, or a lone { that was tolerated before, is a syntax error with u on. If a pattern that worked suddenly does not compile, the flag is the first thing to check.

Four patterns people actually come for

Whitespace and newlines. \s+ collapses runs of spaces, tabs and line breaks. To split a text file into lines regardless of whether they end in \n or \r\n, use \r?\n — a bare \n leaves a stray carriage return at the end of every line.

IPv4 addresses. (\d{1,3}\.){3}\d{1,3} matches the shape but accepts 999.999.999.999. If the value matters, check the ranges in code instead of trying to encode 0–255 three times in a regular expression; the pattern gets five times longer and is still easier to get wrong.

Phone numbers. The same trade. A pattern that accepts an optional country code, spaces, dashes and parentheses is easy to write and will accept numbers of the wrong length. Strip the separators first, then check the digit count.

Email addresses. Full RFC 5322 syntax is famously not worth matching — the specification permits quoted strings and comments that no signup form should accept. A practical check is [^@\s]+@[^@\s]+\.[^@\s]+, which catches missing @ signs and missing dots. Send a confirmation email for the rest; that is the only real validation.

Patterns and text stay in the tab

Regex testers are usually used on real data — log lines, CSV rows, customer records. That text often contains exactly what you should not paste into a stranger's website. Here the match runs in JavaScript on the page: no pattern, no text and no result is sent anywhere, and nothing is stored between visits.

Frequently asked questions

Why does my pattern work in Python but not in JavaScript?

Almost always one of three things: a named group written (?P<name>...), which JavaScript spells (?<name>...); a variable-width lookbehind, which Python rejects and JavaScript allows; or an atomic group, which JavaScript does not have. If the pattern came from Go or another RE2-based language, lookbehind and backreferences are unavailable by design.

Why does test() return true and then false for the same string?

Because the pattern has the g flag. A global regex remembers the position of the last match in lastIndex and continues from there, so repeated calls walk through the string and return false once there is nothing left. Reset lastIndex = 0, or drop the flag for a pure yes-or-no test.

What is catastrophic backtracking?

A pattern with nested quantifiers such as (a+)+$ forces the engine to try every way of splitting a run of characters, and the work grows exponentially with length. It is how a short pattern can freeze a server. Flatten the nesting, bound the input, or use a linear-time engine that does not backtrack.

Do I have to double the backslashes?

Only when the pattern is inside a string rather than a regex literal. In JavaScript, /\d+/ is fine but new RegExp("\d+") is not, because the string parser consumes the backslash first; write "\\d+". The same applies inside JSON, which is why a pattern stored in a config file often appears with doubled backslashes.

Is my text uploaded anywhere?

No. The pattern is compiled and run in your browser, and neither the pattern nor the text is sent to a server, logged or stored. That matters when the text is a log line or a customer record.

Advertisement