How to test a regular expression
- Type your pattern without the surrounding slashes, and choose its flags.
- Paste the text to test against. Matches are highlighted as you type.
- Read each match's position and groups in the table under the text.
- Switch to Replace to try a replacement with
$1or$<name>, then copy the result.
Patterns run in JavaScript's own engine, so a pattern that works here works the same inString.prototype.match, replace and RegExp.prototype.exec. Test text can be up to 200,000 characters, and up to 1,000 matches are shown.
Flags
| Flag | Name | Effect |
|---|---|---|
g | global | Find every match, not just the first |
i | ignore case | a matches A |
m | multiline | ^ and $ match at the start and end of each line |
s | dotAll | . also matches line breaks |
u | unicode | Treats emoji and other astral characters as one character and enables \p{…} |
y | sticky | Each match must start exactly where the last one ended |
Quick reference
| Syntax | Matches |
|---|---|
\d, \w, \s | A digit, a word character (letter, digit or _), whitespace |
[abc], [^abc] | One of a, b or c; anything except them |
a*, a+, a?, a{2,4} | Zero or more, one or more, optional, two to four |
a+? | Lazy: as few as possible |
^, $, \b | Start, end, a word boundary |
(abc), (?<name>abc), (?:abc) | Numbered group, named group, group without capturing |
a(?=b), a(?!b) | a followed by b; a not followed by b |
(?<=b)a, (?<!b)a | a after b; a not after b |
\p{L} (with u) | Any letter, in any alphabet |
Slow patterns
JavaScript's engine backtracks: when part of a pattern fails, it goes back and tries other ways to match. Nested quantifiers such as (a+)+$ or (\w+\s?)*$ can make the number of attempts explode on text that almost matches, which is known as catastrophic backtracking, or ReDoS when it's exploited. The tester stops any run that takes more than one second and tells you. Make each part of the pattern match less, for example [^"]* instead of .*, and avoid quantifiers inside quantified groups.
Common uses
- Building a validation pattern. Paste sample inputs, good and bad, one per line, and refine a pattern with the m flag until it matches exactly the lines it should.
- Pulling data out of text. Write a pattern with named groups to extract dates, IDs or prices from a log or an email, and read each group's value and position in the match list.
- Reformatting with find and replace. Rearrange text with groups, such as turning 2026-10-31 into 31/10/2026, check the result in Replace mode, and copy it out.