Regex Tester
Test a regular expression, see every match and preview a replacement.
About this tool
Uses your browser’s own JavaScript regular expression engine, so results match what you get in JavaScript and Node.js code. Shows up to 500 matches with their positions and capture groups, and previews String.replace with your replacement. Nothing is sent to a server.
Regular expressions (regex) are patterns for finding text: phone numbers, email addresses, dates or anything with a predictable shape. Writing them is easier when you can see the matches as you type. Enter a pattern and some test text to see every match, where it starts and what each capture group caught, and preview how a find and replace would change the text.
How to use the Regex Tester
- Type your pattern without the surrounding slashes, for example \d+ rather than /\d+/.
- Add flags: g finds every match, i ignores case, m makes ^ and $ work per line and s lets the dot match new lines.
- Paste the text you want to test.
- Optionally enter a replacement using $1, $2 or $<name> for groups, and check the result.
Reformatting phone numbers
The pattern (\d{3})-(\d{4}) with the g flag finds 555-0134 and 555-0199 in the sample text, at positions 5 and 17. Each match has two capture groups: 555 and 0134 for the first, and 555 and 0199 for the second. Replacing with ($1) $2 turns the text into Call (555) 0134 or (555) 0199 before 6pm.
Regex building blocks
\d matches a digit, \w a letter, digit or underscore, \s any whitespace and the dot any character except a new line. Quantifiers say how many: * is zero or more, + is one or more, ? is optional and {3} is exactly three. Square brackets make a set, so [aeiou] matches any vowel and [^0-9] anything that is not a digit. ^ and $ anchor the start and end, and \b marks a word boundary. Round brackets create a capture group you can reuse as $1 in a replacement, and (?<year>\d{4}) gives it a name. Add a ? after a quantifier, as in .+?, to make it match as little as possible. Characters with special meanings, such as . * + ? ( ) [ ] { } | \ ^ and $, need a backslash to be matched literally.
Frequently asked questions
Which regex flavour does this use?
JavaScript, as built into your browser. Most syntax is shared with Python, PHP and Java, but some features, such as lookbehind support or possessive quantifiers, differ between languages.
Why do I only see one match?
Without the g (global) flag a regular expression stops after the first match. Add g to the flags to find them all.
How do I match a full stop or a bracket?
Put a backslash in front: \. matches a literal full stop and \( a literal opening bracket.
Why did the page freeze?
Some patterns, such as (a+)+ run against long text, can take an extremely long time to fail. This is called catastrophic backtracking; simplify nested quantifiers to avoid it.