FA Dev Tools

Regex

Test a JavaScript regular expression against your own text. Matches are highlighted as you type, with every capture group, a replace preview, a list of the matches and a split. Nothing leaves your browser.

//g

Type a pattern to start.

/(?:)/g
Flags

Common patterns

$& whole match · $1…$99 group · $<name> named group · $` text before · $' text after · $$ a literal $.

Result

The replaced text appears here.
Instructions

A tester for JavaScript regular expressions — the same engine your browser and Node use. Everything runs in your browser; the pattern and text are kept in this browser's local storage so they survive a reload.

  1. Type a pattern between the slashes. Leave out the slashes and flags — pick flags with the toggles below (g is on by default).
  2. Paste the text to test into Test text. Matches are highlighted as you type.
  3. Matches lists each match with its position and capture groups. Replace previews text.replace(), List puts every match (or one group) on its own line, and Split shows text.split().
  4. Copy the result, or copy the pattern as a /…/flags literal ready to paste into code.

Tips

  • This is JavaScript's dialect. PCRE-only syntax such as possessive quantifiers (a++), atomic groups (?>…) and inline flags (?i) is a syntax error here, and Python's (?P<name>…) is written (?<name>…).
  • Backslashes are typed once: write \d, not the \\d you would need inside a string passed to new RegExp().
  • Without g only the first match is found. Without m, ^ and $ match only at the very start and end of the whole text, not at each line.
  • A pattern that runs longer than a second is stopped, so something like (a+)+$ cannot freeze the page. That usually means nested quantifiers are backtracking; make the inner part more specific.
  • A pattern that can match nothing (a*, \b) shows empty matches as thin carets in the highlighted text.
  • Hit Example, then click a Common patterns chip, Load example, or one of the Replace, List and Split tabs to see it run on sample text.

Cheat sheet

.Any character except a line break (any at all with s)
\d \w \sDigit, word character [A-Za-z0-9_], whitespace
\D \W \SThe opposite of each
\b \BWord boundary, not a word boundary
^ $Start / end of the text (of each line with m)
[abc] [^abc] [a-z]One of, none of, a range
* + ?0 or more, 1 or more, 0 or 1
{3} {2,} {2,5}Exactly 3, 2 or more, 2 to 5
*? +? ??Lazy — as few as possible
(…)Capture group, $1 in a replacement
(?<name>…)Named group, $<name> in a replacement
(?:…)Group without capturing
\1 \k<name>Backreference — the same text again
a|bEither
(?=…) (?!…)Followed by, not followed by
(?<=…) (?<!…)Preceded by, not preceded by
\p{L} \p{Lu}Unicode letter, upper-case letter (needs u or v)

About JavaScript regular expressions

What this tester runs

This page tests JavaScript regular expressions: the RegExp engine built into your browser and Node.js. The pattern is compiled with new RegExp(pattern, flags), so a pattern that works here behaves the same in front-end code, a Node script or a replace() call.

Results come in four views, each with its own URL. Matches lists every match with its position and capture groups. Replace previews text.replace(). List puts each match, or one chosen group, on its own line, optionally without duplicates. Split shows text.split(), including any pieces caught by capture groups, as the real call returns them.

The pattern runs in a Web Worker. If it runs for more than one second it is stopped, so a runaway pattern cannot freeze the tab. The first 1,000 matches are listed. Your pattern and text are kept in this browser's local storage and are never uploaded.

The flags, and why Unicode matters

JavaScript has eight flags, and each is a toggle here. g finds every match instead of the first. i ignores case. m makes ^ and $ match at every line break, not only at the start and end of the text. s lets . match line breaks too. u reads the pattern and text as Unicode code points and enables \p{…} property classes. v builds on u with set operations inside classes, such as [\p{L}--[a-z]], and cannot be combined with it. y (sticky) matches only at the current position. d records where each capture group starts and ends.

Without u, an emoji is two UTF-16 code units, so . matches half of it. Even with u, \d and \w stay ASCII-only: \w+ stops at the é in “café”. For letters and digits in any script, use \p{L} and \p{Nd} with the u flag.

Coming from PCRE, Python or another flavour

Most syntax carries over: classes, quantifiers, lazy quantifiers, lookahead, lookbehind, named groups and backreferences. The differences that catch people out are small but strict. Named groups are written (?<name>…), not Python's (?P<name>…). In a replacement, groups are $1 and $<name>, not \1; $& is the whole match and $$ is a literal dollar sign.

Possessive quantifiers (a++), atomic groups (?>…), recursion and a leading (?i) are not JavaScript syntax; use the i flag instead of (?i). There is no \A or \z: use ^ and $ with m off. Beware that without u, an unknown escape such as \A quietly matches the letter A instead of raising an error.

Backslashes are typed once here. In source code, a pattern passed as a string to new RegExp() needs each backslash doubled ("\\d+"), while a literal /\d+/ does not. The tool shows a ready-to-copy literal under the pattern box.

Catastrophic backtracking

Browser regex engines backtrack: when part of a pattern fails, they go back and try every other way the earlier parts could have matched. Usually that takes microseconds. With nested quantifiers over the same characters, the number of ways explodes. (a+)+$ tested against thirty as and then a b must try hundreds of millions of ways to split the run before it can report no match, and each extra a roughly doubles the work. On a server this is a denial-of-service bug known as ReDoS.

The fix is to remove the ambiguity so each character can be matched only one way. (a+)+$ means the same as a+$. (\w+\s?)+ can split a word many ways because the space is optional, while \w+(?:\s+\w+)* needs a space between repeats. Inside quotes, "[^"]*" is safer than ".*".

Frequently asked questions

Why does my regex only find the first match?
The g (global) flag is off. Without it, the engine stops after the first match. In code, str.match(/x/) returns the first match with its groups, str.match(/x/g) returns every match but no groups, and str.matchAll(/x/g) gives you both.
Why does my regex work here but not in Python, Java or grep?
Each language has its own flavour. Python names groups with (?P<name>…), Java strings need doubled backslashes, and grep defaults to POSIX basic syntax, where \d is not supported and + is a literal unless you use grep -E (or grep -P for Perl syntax). Test in the flavour you will run.
How do I match a dot, bracket or other special character literally?
Put a backslash before it. Outside a class, . * + ? ^ $ { } ( ) | [ ] and \ are special, so \. matches a dot and \( a bracket. Inside [ ], only ] \ ^ and - need care (the v flag adds a few more). A / does not need escaping in the pattern box, but it does in a /…/ literal; the copied literal escapes it for you.