What Is a Regular Expression
A regular expression, or regex, is a tiny language for describing patterns in text. It is older than almost any mainstream programming language and now lives inside every editor, log analyzer, scraper and form validator. Once you learn the symbols, a single line of regex can replace twenty lines of imperative string code. The flip side is that regex is notoriously easy to get wrong, which is why a live tester like this one speeds up debugging.
How to Use This Tool
- Type a pattern. No slashes needed, just the pattern body.
- Pick flags: g for all matches, i for case insensitive, m for multiline, s to let dot cross newlines, u for full unicode.
- Paste or type the text you want to match against. Matches are highlighted in yellow as you type.
- Optionally add a replacement string with $1, $2 or $<name> placeholders to preview the output.
- Scroll down for the details pane which lists every match offset and every capture group.
Common Patterns
- Email, roughly:
\b[\w.+-]+@[\w-]+(?:\.[\w-]+)+\b - URL:
https?://[\w.-]+(?:/[\w./?&=%-]*)? - Phone (very flexible):
\+?\d[\d\s().-]{7,}\d - IPv4:
\b(?:\d{1,3}\.){3}\d{1,3}\b(does not validate range) - Hex color:
#(?:[0-9a-fA-F]{3}){1,2}\b
Greedy vs Lazy
By default quantifiers are greedy. The pattern <.*> against the text <a><b> matches the entire string because .* gobbles characters until the last >. Add a question mark to make the quantifier lazy: <.*?> matches <a> and then <b> separately. Lazy quantifiers are a frequent fix when a regex matches too much.
(a+)+$ against a long input of all a letters can freeze a browser as the engine explores exponential combinations. Keep quantifiers focused and prefer non-capturing groups when you do not need the captured value.
Lookaround
Lookahead (?=...) and negative lookahead (?!...) let you assert what follows without consuming characters. Lookbehind (?<=...) does the same for what came before. Modern browsers support all four. They are useful for conditions like matching a word only when it is not followed by a comma.
Privacy
Everything executes locally in your browser. No pattern or test text is uploaded or stored.
Frequently Asked Questions
Which regex flavor?
JavaScript (ECMAScript). Close to PCRE but not identical.
What do g, i, m, s, u mean?
Global, case insensitive, multiline, dotall and full unicode.
How are groups numbered?
Left to right by opening parenthesis. Group 0 is the full match.
Why does my pattern match too much?
Quantifiers are greedy. Make them lazy with a trailing ?.
Is my input private?
Yes. Everything runs in the browser.
Can I preview a replacement?
Yes. Enter a replacement string with $1 or $<name> placeholders.