MiServe DevTools

Your lid

Pin tools here. They are saved in this browser.

Regex Tester for JavaScript and Go (RE2)

Test a regular expression against your text with the JavaScript engine or with Go's RE2 engine, and see every match, capture group and replacement.

Flags

Matches in the text

Syntax cheat sheet for both engines
TokenMeaningJavaScriptGo (RE2)
. Any character except a line break. With the s flag it matches line breaks too. Yes Yes
\d \w \s A digit, a word character, a white space character. The capitals \D, \W and \S match the opposite. Yes Yes
[abc] [^abc] [a-z] One character from a set, one character not in the set, one character in a range. Yes Yes
^ $ Start and end of the text. With the m flag, start and end of every line. Yes Yes
\b \B A word boundary, and a position that is not a word boundary. Yes Yes
* + ? {2,5} Zero or more, one or more, zero or one, between 2 and 5 times. Each takes as much as it can. Yes Yes
*? +? ?? {2,5}? The same quantifiers, taking as little as they can. Yes Yes
(…) (?:…) A capture group, and a group that only groups. Yes Yes
(?<name>…) A named capture group. Go accepts this form since version 1.22, and (?P<name>…) in every version. Yes Yes
a|b Either a or b. Yes Yes
\p{L} \p{Lu} A Unicode property: any letter, any uppercase letter. JavaScript needs the u flag for it. Yes Yes
(?i) (?s) (?m) Flags written inside the pattern. In JavaScript, flags are set outside it. No Yes
\A \z Start and end of the whole text, whatever the m flag says. No Yes
[[:alpha:]] A POSIX character class inside a set. No Yes
(?=…) (?!…) Lookahead: what follows must match, or must not match, without being consumed. Yes No
(?<=…) (?<!…) Lookbehind: what comes before must match, or must not match. Yes No
\1 \k<name> Backreference: the same text an earlier group matched. Yes No

Runs in your browser with WebAssembly. Nothing you paste is uploaded.

About this tool

Two engines from two families

Regular expression engines come in two families. Backtracking engines, such as PCRE and the default engines of JavaScript, Python, Java and .NET, try one path through the pattern and step back when it fails. That design makes lookahead, lookbehind and backreferences possible. Automaton engines, such as RE2 and Go's regexp package, follow all the possible paths at once, so their running time grows linearly with the length of the text. The price is that lookaround and backreferences are not available.

The JavaScript engine here is your browser's own RegExp. The Go engine is Go's standard regexp package compiled to WebAssembly, so the code that runs your pattern here is the code that runs it in a Go program. Go accepts the syntax of RE2.

Reading the results

Each match is outlined and highlighted in the text, in two alternating colours so that matches that touch can be told apart. An empty match, such as the one x* finds between two letters, is drawn as a thin bar. The table lists the start and end of every match and the text of every capture group. A group that took no part in a match is marked as not matched, which is different from a group that matched an empty string.

Positions count UTF-16 code units from zero, and the end is exclusive: they are the numbers you pass to String.prototype.slice in JavaScript. Go itself reports byte offsets into UTF-8 text; the tool converts them, so both engines show the same numbers.

In JavaScript, a pattern without the g flag finds only the first match and replaces only the first match. The Go engine always reports every match, as FindAllStringSubmatchIndex and ReplaceAllString do.

Catastrophic backtracking

A backtracking engine can need exponential time when quantifiers are nested or overlap. The classic case is (a+)+$ tested against a long run of a followed by another character: the engine tries every way of splitting the run between the inner and the outer quantifier before it gives up. On a server this is a denial-of-service risk known as ReDoS.

This tool runs JavaScript patterns in a background worker and stops the worker after 1.5 seconds, so the page stays responsive and tells you what happened. Testing the same pattern with the Go engine is a quick way to see whether the slowness comes from backtracking, because Go guarantees a running time that is linear in the length of the text.

Questions

Why does my pattern work in JavaScript but fail in Go?

A common cause is lookahead, lookbehind or a backreference, which RE2 leaves out so that it can guarantee linear running time. Flags are another common cause: Go has no flags outside the pattern, so case-insensitive matching is written as (?i) at its start.

How do replacement strings differ between JavaScript and Go?

Both use $1 for the first group. A named group is $<name> in JavaScript and ${name} in Go. In Go, $1x means a group called 1x, so write ${1}x when a letter, digit or underscore follows the reference.

Can I test PCRE, Python or Java patterns here?

Only the JavaScript and Go engines run on this page. The everyday syntax is shared between flavours, so the JavaScript engine is a good approximation for simple patterns, but features such as possessive quantifiers, atomic groups and recursion are not available in either engine here.

Is the text I test sent to a server?

No. Both engines run in your browser: JavaScript in a background worker, Go as a WebAssembly module. The tool makes no network request with your pattern or your text, and the site's security policy blocks requests to any other host.

More in Text