How to test a regex
- Type the pattern without the surrounding slashes.
- Choose flags and paste the text to search.
- Check the highlighted matches and groups, and try a replacement.
Building blocks
\d is a digit, \w a word character, \s whitespace, . any character, ^ and $ the start and end. Quantifiers * + ? and {n,m} set how many times, and parentheses create groups.
Greedy and lazy
By default .* matches as much as possible. Add ? (.*?) to match as little as possible, for example to take one HTML tag at a time instead of everything between the first < and the last >.
Write patterns that stay fast
Avoid nested quantifiers like (a+)+ and overlapping alternatives, anchor patterns with ^ and $ when you can, and prefer specific classes like [^"]* over .*.
Frequently asked questions
Which regex flavor is used?
JavaScript (ECMAScript), the same engine as your browser and Node.js. Most syntax also works in Python, Java and PCRE, with small differences.
How do I reference groups in the replacement?
Use $1, $2 for numbered groups, $<name> for named groups and $& for the whole match.
Why does my pattern only match once?
Without the g (global) flag, only the first match is found.
Why did the page freeze?
Some patterns with nested quantifiers, like (a+)+, take exponential time on certain input. Simplify the pattern or make it more specific.
