How to use the regular expression tester
Use this regex tester online as a scratchpad: check regex ideas against real text before they go into your code.
- Enter the pattern between the slashes, without the slashes themselves. Errors such as an unclosed group are reported as you type.
- Tick flags. Without
gonly the first match is found, which is howRegExp.execandString.matchbehave too. - Paste the test string. Matches alternate between two colours so neighbouring matches are easy to tell apart.
- Turn on Replace to preview
String.replacewith$1,$<name>or$&(the whole match).
Regex cheat sheet
The most used tokens in JavaScript regular expressions. The full reference is the MDN regular expression cheat sheet, and the grammar itself is defined in the ECMAScript specification.
Characters and classes
. | Any character except a line break (any at all with the s flag) |
\d \D | A digit 0–9 / not a digit |
\w \W | A word character [A-Za-z0-9_] / not one |
\s \S | Whitespace (space, tab, line break…) / not whitespace |
[abc] | One of a, b or c |
[^abc] | Any character except a, b or c |
[a-z] | A character in the range a to z |
\p{L} | Any Unicode letter (needs the u flag) |
\. \* \\ | A literal dot, asterisk or backslash |
Quantifiers
* | Zero or more |
+ | One or more |
? | Zero or one (optional) |
{3} | Exactly 3 |
{2,5} | Between 2 and 5 |
{2,} | 2 or more |
*? +? ?? | Lazy versions: as few as possible |
Anchors and boundaries
^ $ | Start / end of input (of each line with the m flag) |
\b \B | Word boundary / not a word boundary |
Groups and lookaround
(abc) | Capture group, referenced as $1 or \1 |
(?<name>abc) | Named group, referenced as $<name> or \k<name> |
(?:abc) | Group without capturing |
a|b | a or b |
(?=abc) (?!abc) | Followed by / not followed by abc |
(?<=abc) (?<!abc) | Preceded by / not preceded by abc |
Flags
g | global | Find all matches, not just the first |
i | ignoreCase | Case-insensitive |
m | multiline | ^ and $ match at line breaks |
s | dotAll | . also matches line breaks |
u | unicode | Unicode code points and \p{…} escapes |
y | sticky | Match only at lastIndex |
d | hasIndices | Record start and end of groups |
Regex examples
Copy any of these into the tester above and change them to fit:
| Pattern | Matches |
|---|---|
^\d{5}(-\d{4})?$ | US ZIP code, with optional +4 |
^#?([0-9a-f]{3}|[0-9a-f]{6})$ | Hex color like #fff or #1e90ff (use i) |
^\d{4}-\d{2}-\d{2}$ | ISO date shape, such as 2026-10-18 |
\b(\w+)\s+\1\b | A doubled word, like “the the” |
^\s+|\s+$ | Leading or trailing whitespace (use g to trim both) |
https?://[^\s/$.?#].[^\s]* | A rough URL finder |
Anchors matter for a regex validator: without ^ and $, \d{5} also matches the first five digits of 1234567. When you check user input, anchor the whole pattern.
JavaScript, Java, Python and PCRE
Regex flavors share most syntax but differ at the edges. This regex checker uses the JavaScript engine, so keep these differences in mind when the pattern is for another language:
- Java (
java.util.regex) supports possessive quantifiers (a++) and atomic groups, which JavaScript does not, and in Java string literals every backslash is doubled:"\\d+". Named groups use the same(?<name>…)syntax. - Python (
re) names groups as(?P<name>…)and sets flags with arguments such asre.IGNORECASEinstead of letters after a slash. - PCRE (PHP, many editors) adds recursion and conditionals that JavaScript lacks.
- A leading inline modifier such as
(?i), common in Java, Python and PCRE, is not supported in JavaScript. ES2025 adds only the scoped form(?i:…), which appliesi,morsto one group (see MDN: modifier), so use the flag checkboxes for the whole pattern.
Catastrophic backtracking
JavaScript's engine backtracks: when part of a pattern fails it goes back and tries other ways to split the text. With nested quantifiers such as (a+)+$, the number of ways grows exponentially with the input length, and a short string of as followed by a b can take seconds or longer. This tester runs every pattern in a background worker and stops it after 1.5 seconds, so the page never freezes, and it warns about nested quantifiers before you run them. In production code, the same pattern can stall a server, a risk known as ReDoS.
Risky: ^(\w+\s?)*$
Safer: ^\w+(\s\w+)*$ (each repetition must consume a word)Comparing two versions of a long text instead of matching a pattern? The text compare tool highlights every difference.