How to use the Regex Tester tool
- Enter a patternType your regular expression between the slashes, or pick one from “Common patterns”.
- 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.
- Add test textPaste the text to search. Matches are highlighted and listed with their position and capture groups.
- 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
| Pattern | Matches |
|---|---|
. | Any character except a line break (any character with the s flag) |
\d \w \s | A digit, a word character (letter, digit, _), whitespace |
\D \W \S | The 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) |
\b | Word boundary |
(…) (?:…) (?<name>…) | Capture group · non-capturing group · named group |
a|b | a or b |
(?=…) (?!…) | Lookahead: followed by · not followed by |
(?<=…) (?<!…) | Lookbehind: preceded by · not preceded by |
\1 | The 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.