Language: English

tools / regex

regex/

Live JavaScript regex tester: highlights matches, lists capture groups and previews replacements.

Presets

Test string

Highlighted matches


      

Match list

#PositionMatchGroups

Replace

What is a regular expression

A regular expression is a small language for describing text patterns; this page uses JavaScript regular expressions as specified by ECMA-262, with six flags you can tick: g, i, m, s, u and y. It finds every match in a background thread, lists the capture groups and previews the result of a replacement template such as $1.

How it works: the browser's RegExp finds one match after another

  1. The pattern and the test string are handed to a background thread in the browser (a Web Worker), compiled with new RegExp and run there; if it backtracks too much, the page stops that thread and the page itself does not freeze.
  2. With g ticked, exec is called repeatedly, each time continuing from the end of the previous match; after an empty match the position moves forward one character (a whole surrogate pair when u is ticked), otherwise it would keep matching in place.
  3. Each match records its start position, the matched text, the capture groups in order and the named groups; replacement passes the same pattern to String.prototype.replace.

Example

The three input lines are the pattern, the test string and the replacement template: $1 is the user name captured by the first parentheses and $2 the domain captured by the second, so both matches become "domain:user".

Input
(\w+)@(\w+)\.com
alice@example.com bob@test.com
$2:$1
Output
example:alice test:bob

Sources

FAQ

Which regex engine does this use, and is it the same as Python or PCRE?

It uses your browser's built-in JavaScript regex engine, so the syntax differs a little from Python, PCRE and Java. Named groups are written (?<name>...) rather than Python's (?P<name>...); inline flags such as a leading (?i) are not supported, so tick the i flag instead; possessive quantifiers and atomic groups do not exist either. Whether newer syntax such as lookbehind works depends on your browser.

Why doesn't a regex I copied from code match?

Most likely the surrounding slashes, the trailing flags or the escaping backslashes came along with it. In the pattern box, type only what sits between the two slashes, and tick flags such as g and i on the right. If you copied from a JavaScript string, change \\d+ to \d+, because the box does not need backslashes escaped a second time.

Why do I only get one match, or why do ., ^ and $ behave oddly?

Check the flags next to the pattern box first. Without g, only the first match is taken, and the list header says so. The dot does not match line breaks unless you tick s; ^ and $ only match the start and end of the whole text unless you tick m for per-line matching. y is sticky mode: matches must start at the beginning and follow one another without gaps, so you rarely need it.

What should I do about the "Matching took over 1.5 s and was stopped" message?

The regex backtracks too much on your text and needs to be rewritten. A typical culprit is a nested quantifier such as (a+)+, whose running time multiplies on a long text that fails to match. The page runs the regex in a background thread and stops it after 1.5 seconds, so the page itself never freezes. Making the quantifiers specific, removing the nesting or using a narrower character range usually fixes it.

What happens when there are many matches or the text is very long?

The highlight and the list show at most the first 1000 matches and tell you when there are more; the replace result is not affected by that cap. When the text is longer than 200,000 characters, the page waits until you stop typing for about 0.4 seconds before recalculating. There is no other length limit, so speed mostly depends on the regex itself.

How do I write the replacement template?

It follows JavaScript's replacement syntax: $1 and $2 are numbered capture groups, $<name> is a named group, $& is the whole match, and $$ is a literal dollar sign. Leave the template empty to delete whatever matched. Without the g flag, only the first match is replaced.

Can the presets be used for validation as they are?

Only as a starting point, not as strict validation: the presets check the format, not whether the value really exists. The date preset accepts days 01 to 31 in every month, ignoring month lengths and leap years; the color preset accepts # followed by 3, 4, 6 or 8 hex digits; the US ZIP code preset accepts 5 digits with an optional -1234 extension and does not check that the code exists. The email and URL presets cover common forms only, and the URL one accepts just http and https. Clicking a preset replaces the pattern in the box and resets the flags to g only.

Is the text I enter uploaded?

No. The regex runs in a background thread of your browser, and the test text, the pattern and the replacement template are never sent to a server. The page does not store your input either.