A regular expression that looks right can still match too much, too little or nothing, and the only reliable check is to run it against real text. Testing it first, with every match and capture group in view, takes a minute and saves a broken data migration or a validation rule that rejects valid input. This guide walks through testing a pattern, the syntax you will use most, using groups in replacements, and the kind of pattern that can hang a program.
Step by step
- Open the regex tester and type your pattern in the Regular expression box, without surrounding slashes:
(\d{4})-(\d{2})-(\d{2}), not/(\d{4})-(\d{2})-(\d{2})/. - Paste sample text into Test text. Use real data, including the awkward cases: empty values, extra spaces, accented letters, the line that broke last time.
- Toggle flags as needed: i to ignore case, m for multiline anchors, s so that
.also matches newlines, and u for full Unicode handling. - Read the result list. Each match shows its text, its position in the input and each capture group as
$1,$2and so on, plus named groups by name. - Adjust the pattern until the matches are exactly what you want, no more and no less.
- When you are ready to change text rather than just find it, move the pattern to find and replace, tick Regular expression and write a replacement.
Both tools use the JavaScript regex engine, the same one used by browsers and Node.js, and run patterns in a background worker. If a pattern takes longer than 1.5 seconds, it is stopped and you see an error instead of a frozen page.
The syntax you will use most
| Pattern | Matches |
|---|---|
. |
Any character except a line break (any character at all with the s flag) |
\d, \w, \s |
A digit, a word character (letter, digit or _), whitespace |
\D, \W, \S |
The opposite of each |
[abc], [^abc], [a-z] |
One of the listed characters, none of them, a range |
*, +, ? |
Zero or more, one or more, zero or one |
{3}, {2,5}, {2,} |
Exactly 3, between 2 and 5, 2 or more |
*?, +? |
Lazy versions: as few as possible |
^, $ |
Start and end of the input (of each line with the m flag) |
\b |
A word boundary |
(…), (?:…) |
A capturing group, a non-capturing group |
(?<name>…) |
A named capturing group |
a|b |
Either a or b |
(?=…), (?!…), (?<=…), (?<!…) |
Lookahead and lookbehind, positive and negative |
Characters with special meaning, . * + ? ^ $ { } ( ) [ ] \ | and /, need a backslash to be matched literally. To match a price like $9.99 you write \$\d+\.\d{2}.
Note that \w and \b only treat ASCII letters, digits and underscore as word characters, even with the u flag. To match words in any script, use Unicode property escapes with the u flag, such as \p{L}+ for a run of letters.
Flags change what a pattern means
The same pattern can give very different results depending on flags. Take this text:
Name: Ada
name: Grace
^name: \w+with no flags finds nothing.^means "start of the input", and the input starts with a capital N.- With i, it finds
Name: Ada, still only the first line. - With i and m,
^matches at the start of every line, so it finds both.
The s flag matters when a match spans lines. <p>.*</p> will not match a paragraph that contains a line break unless s is on. The u flag makes the engine treat characters outside the Basic Multilingual Plane, such as many emoji, as one character instead of two, and enables \p{…}.
Capture groups and backreferences in replacements
Capture groups pay off when you replace text. In find and replace, with Regular expression on, you can insert parts of each match:
$1,$2: the first, second capture group$<name>: a named group$&: the whole match
To convert ISO dates to day/month/year:
Find: (\d{4})-(\d{2})-(\d{2})
Replace: $3/$2/$1
2026-11-23 becomes 23/11/2026. With named groups the replacement is easier to read:
Find: (?<y>\d{4})-(?<m>\d{2})-(?<d>\d{2})
Replace: $<d>/$<m>/$<y>
Inside the pattern itself, \1 refers back to what group 1 matched. \b(\w+)\s+\1\b finds doubled words such as "the the", and replacing with $1 removes the duplicate.
Two details to get right. First, replacement syntax uses $, while backreferences inside a pattern use \. Second, in plain-text mode (regex off), the replacement is inserted exactly as typed, so $1 stays the literal text $1. Use Match case to control case sensitivity, Whole word to avoid replacing inside longer words, and turn off Replace all to change only the first match. The match count updates as you type, so you can see how many replacements you are about to make. To check the outcome against the original, paste both into text compare.
Catastrophic backtracking
Most regex engines, including JavaScript's, are backtracking engines. When a match fails, they go back and try other ways of dividing the input between quantifiers. Usually there are few alternatives. With nested or overlapping quantifiers there can be exponentially many.
Pattern: ^(a+)+$
Input: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa!
The ! means the match must fail, but before giving up the engine tries every way of splitting the run of a characters between the inner and outer +. Each extra a roughly doubles the work. A few dozen characters are enough to keep it busy for a very long time, which is why the tester stops patterns after 1.5 seconds. In a server, the same pattern applied to user input is a denial-of-service risk known as ReDoS.
Warning signs in a pattern:
- A quantified group that contains a quantifier:
(a+)+,(\w*)*,(\d+\s?)+ - Alternatives that can match the same text:
(a|a)*,(\w|\d)+ - Several adjacent quantifiers that compete for the same characters:
\s*.*\s*
Fixes, in order of preference:
- Remove the nesting.
^(a+)+$means the same as^a+$. - Make the alternatives mutually exclusive, so each character can be matched in only one way. For example
"[^"]*"for a quoted string: the class can never match the closing quote, so there is only one way to match. - Anchor the pattern and be specific about what each part may contain.
JavaScript has no possessive quantifiers or atomic groups, which some other engines use to prevent backtracking. If you copy a pattern from PCRE, Java or .NET documentation, test it here before relying on it.
Tip When a pattern times out in the tester, shorten the test input until it completes. If doubling the input length makes it many times slower, the pattern backtracks catastrophically and should be rewritten.