Regular expression tester
Write a pattern, see every match highlighted in your test text, and inspect capture groups as you type.
Runs entirely in your browser. Nothing is uploaded.
Regular expression building blocks
| Pattern | Matches |
|---|---|
. | Any character except a newline |
\d \w \s | Digit, word character, whitespace |
\D \W \S | The inverse of each |
[abc] | Any one of a, b or c |
[^abc] | Any character except those |
* + ? | Zero or more, one or more, optional |
{2,5} | Between two and five repetitions |
^ $ | Start and end of the string, or of each line with the m flag |
\b | A word boundary |
(…) | A capture group, available as $1, $2 and so on |
(?:…) | A group that does not capture |
a|b | Either alternative |
Greedy and lazy matching
Quantifiers are greedy by default: <.+> against <b>hi</b> matches the entire string, because .+ takes as much as it can and then backtracks. Adding a question mark makes it lazy — <.+?> matches just <b>. This single distinction explains most "my pattern matches too much" problems.
Patterns worth keeping
- Trim whitespace:
^\s+|\s+$ - Collapse repeated spaces:
\s{2,} - ISO date:
\d{4}-\d{2}-\d{2} - Hex colour:
#?([0-9a-fA-F]{3}|[0-9a-fA-F]{6})\b - Strip HTML tags, crudely:
<[^>]+>
Where regular expressions are the wrong tool
Do not parse HTML, XML or JSON with them — those formats nest, and regular expressions cannot track nesting reliably. Use a parser. Email validation is another trap: the fully correct pattern is enormous, and the practical answer is to check for an @ with something either side and then send a confirmation message.
Watch out for catastrophic backtracking too. Nested quantifiers such as (a+)+b can take exponential time on a long non-matching input, which is a denial-of-service risk when a pattern is applied to user input on a server.
Questions
Which flavour of regex is this?
JavaScript's, as implemented by your browser. Python, PCRE and Go differ in lookbehind support and a few escape sequences, though the basics shown here are the same everywhere.
Why does my global pattern loop forever?
A pattern that can match an empty string never advances the index. This tool guards against it; in your own code, increment lastIndex when a match has zero length.
More tools
Browse all free developer tools or read the guides.