JavaScript Regex Tester

Test a JavaScript regular expression, highlight matches, list capture groups, and try a replacement.

A JavaScript regular expression, tested on your text

This tester uses the regular expression engine in your browser, which is the ECMAScript flavor. It is what JavaScript, JSON Schema patterns and many front-end validators use. It is not PCRE. Lookbehind works in current browsers. A pattern copied from a PHP preg_match manual can still fail here if it uses a PCRE-only verb.

The global flag is always on, so you see every match, not only the first. That matches what people expect from a tester. The replacement box uses JavaScript replacement rules: $1, $2, and $$ for a dollar sign.

If you only need to pull matches out of a file and you already know the pattern works, the regex extract tool is the shorter path.

How to use it

  1. Type the pattern without slashes. \d+ is enough. Do not wrap it in /\d+/g; flags are the checkboxes.
  2. i ignores case. m lets ^ and $ match at each line, not only at the ends of the whole text. s lets . match a newline. u turns on Unicode mode, which you want for \p{L} and for astral characters.
  3. Paste the text. Matches are highlighted and listed with the index and each capture group. An empty group is shown as ∅, which is different from a group that captured an empty string in some engines; here ∅ means the group did not participate.
  4. Turn on replacement and use $1 for the first group. The result is the whole text after substitution, not only the matched pieces.
  5. An invalid pattern shows the browser’s error. A common one is an unclosed ( or a dangling \.

A short reminder of the syntax

  • . any character except newline, unless s is on. \d digit, \w word character, \s whitespace. Capital forms are the opposite.
  • + one or more, * zero or more, ? optional. Add ? after those to make them lazy: +?.
  • [a-z] a set. [^0-9] is the set’s opposite. a|b is either branch.
  • (...) captures. (?:...) groups without capturing. The list on this page numbers capturing groups from 1, same as $1.
  • ^ and $ are the ends of the text, or of each line when m is on. \b is a word boundary.

Questions

Why did a trailing comma disappear from the match?

The pattern did not include it. The highlight is only what the expression consumed. Look at the groups if you expected a piece to be captured separately.

The pattern works in my editor and fails here.

Check the flavor. Editors often use PCRE or a Unicode build where \w includes accented letters. In JavaScript, \w is [A-Za-z0-9_] unless you use Unicode property escapes with the u flag.

Can this hang the tab?

A nested quantifier such as (a+)+ on a long failing string can be very slow. The page stops after 5000 matches, but it cannot make a catastrophic pattern fast. Narrow the pattern.