Skip to content
All tools

JavaScript Regex Tester

Test a regular expression against your text with live highlighting, see every group, try replacements, and read the pattern explained piece by piece.

How it works

The pattern runs on your browser's own RegExp, so the result is exactly what your JavaScript code gets. That also means this is JavaScript syntax: no PCRE extras like possessive quantifiers or (?x) comments, and Python's (?P<name>…) is written (?<name>…) here.

Nothing leaves the page. The text is matched in a Web Worker on your device, and a share link carries only the first 2,000 characters of it.

The flags

FlagNameWhat changes
gglobalAll matches instead of the first. Needed for matchAll() and replaceAll().
iignore casea matches A.
mmultiline^ and $ match at every line break, not only at the ends of the text.
sdotAll. also matches line breaks.
uunicodeEmoji and other astral characters count as one character, p{L} works, and invalid escapes become errors.
vunicodeSetsEverything u does, plus nested classes and && / -- inside them. Can't be combined with u.
ystickyMatches only at lastIndex, without searching ahead. Used in tokenizers.
dindicesAdds m.indices with the start and end of every group.

Groups and replacements

(…) captures, (?:…) only groups, and (?<name>…) captures under a name you read as m.groups.name. In a replacement string:

  • $1, $2 insert numbered groups, $<name> a named one.
  • $& is the whole match, $` the text before it and $' the text after it.
  • $$ is a literal dollar sign.

The ISO date example turns 2024-03-15 into 15.03.2024 with $<day>.$<month>.$<year>.

Catastrophic backtracking

When a pattern fails, the engine goes back and tries every other way to split the text between its quantifiers. Nested quantifiers make that number explode. ^(a+)+$ on thirty as followed by ! has over a billion ways to fail, and a running RegExp can't be interrupted: the tab freezes.

That's why matching here runs in a worker that is terminated after a second. Try the "Backtracking trap" example. The fixes are usually simple:

  • Remove the nesting: ^a+$ matches the same text.
  • Make the alternatives exclusive, so the engine has only one way to read each character.
  • Never run user-supplied patterns on your server's main thread. Validate input length first, or use an engine without backtracking such as RE2.

Reading the explanation

Each line describes one piece of the pattern, indented by nesting. Hover a line to see which part of the pattern it is about. Quantifiers are read together with what they repeat, so d{4} becomes "a digit (0–9), exactly 4 times", and lazy quantifiers (+?, *?) add "as few as possible".