Regex Tester

Test JavaScript regular expressions live. Matches are highlighted as you type, capture groups are listed in a table, and you can preview find-and-replace results instantly.

  • Live highlighting
  • Groups & named groups
  • Replace preview
//
Matches

How to use the Regex Tester tool

  1. Enter a patternType your regular expression between the slashes, or pick one from “Common patterns”.
  2. Set the flagsTick g for all matches, i to ignore case, m for multiline anchors, s so the dot matches new lines, and u for Unicode.
  3. Add test textPaste the text to search. Matches are highlighted and listed with their position and capture groups.
  4. Try a replacementType a replacement using $1, $2 or $<name> to preview the result, then copy it or the regex as JavaScript.

Features

  • Real-time resultsHighlights and the match table update as you type the pattern or the text.
  • Capture groupsSee every numbered and named group for each match.
  • Find and replacePreview replacements with backreferences before using them in your code or editor.
  • Safe testingPatterns run in a background worker and are stopped after 1.5 seconds, so a runaway regex can’t freeze the page.
  • Pattern libraryReady-made expressions for emails, URLs, dates, phone numbers, IP addresses and more.

What is a regular expression?

A regular expression (regex) is a compact pattern that describes text. It lets you find, validate and transform text in ways simple search can’t: every email address in a document, dates in any format, duplicate words, or all lines that start with a number. Regex is supported in almost every programming language, code editor, database and spreadsheet app.

Regex cheat sheet

PatternMatches
.Any character except a line break (any character with the s flag)
\d \w \sA digit, a word character (letter, digit, _), whitespace
\D \W \SThe opposite of the above
[abc] [^abc] [a-z]One of a, b or c · anything except them · a range
* + ?0 or more · 1 or more · 0 or 1 (optional)
{3} {2,5} {2,}Exactly 3 · between 2 and 5 · 2 or more
*? +?Lazy versions: match as little as possible
^ $Start and end of the text (of each line with the m flag)
\bWord boundary
(…) (?:…) (?<name>…)Capture group · non-capturing group · named group
a|ba or b
(?=…) (?!…)Lookahead: followed by · not followed by
(?<=…) (?<!…)Lookbehind: preceded by · not preceded by
\1The same text as group 1 again (backreference)

Flags explained

  • g (global): find all matches instead of stopping at the first.
  • i (ignore case): “abc” also matches “ABC”.
  • m (multiline): ^ and $ match at the start and end of every line.
  • s (dotAll): the dot also matches line breaks.
  • u (unicode): full Unicode support, needed for \p{L} (any letter in any language) and emoji.

Avoiding catastrophic backtracking

Some patterns, such as (a+)+$, can take exponentially long on text that almost matches, freezing apps and even taking servers down (a “ReDoS” attack). Avoid nested quantifiers on the same characters, prefer specific classes like [^"]* over .*, and anchor patterns where you can. This tester stops any pattern that runs for longer than 1.5 seconds, so you can spot the problem safely.

Regex flavors

This tool uses your browser’s JavaScript (ECMAScript) engine. Most syntax is the same in Python, PHP (PCRE), Java and .NET, but details differ. For example, JavaScript uses $1 in replacements where Python uses \1, and some languages support possessive quantifiers that JavaScript doesn’t. To check exactly what a replacement changed, paste the before and after text into the Text Diff Checker.

Frequently asked questions

Which regex flavor does this tester use?
JavaScript (ECMAScript), as built into your browser, including named groups, lookbehind and Unicode property escapes.
Why does my pattern only find one match?
Turn on the g (global) flag. Without it, a regular expression stops after the first match.
How do I use capture groups in a replacement?
Use $1, $2 and so on for numbered groups, $<name> for named groups and $& for the whole match.
Why was my regex stopped?
It ran for more than 1.5 seconds, usually because of catastrophic backtracking from nested quantifiers like (a+)+. Make the pattern more specific.
Is my test text sent anywhere?
No. Matching happens in your browser and nothing is uploaded or saved.