What Is a Regex Tester?
A regular expression (regex or regexp) is a compact pattern that describes text: "an @ sign between two groups of letters", "four digits, a dash, two digits", "a word repeated twice". Regexes are used everywhere in software for validating form input, searching and replacing in editors, parsing log files, extracting data, routing URLs and cleaning up text. They're powerful but notoriously easy to get wrong, which is why a regex tester that shows matches live is invaluable.
This tester uses your browser's own JavaScript regex engine, so the results are exactly what you'll get in JavaScript, TypeScript and Node.js, and are very close to most other languages for common patterns.
How to Use the Regex Tester
- Type your pattern between the slashes, or pick one from the library.
- Choose flags:
gfinds all matches,iignores case,mmakes ^ and $ match at line breaks,slets . match newlines,uenables full Unicode, andyis sticky. - Paste your test text. Matches are highlighted instantly, in alternating colours so adjacent matches are easy to see.
- Inspect the table for each match's position and its capture and named groups.
- Try a replacement using
$1,$<name>or$&(the whole match). - Read the explanation to check each token does what you intended, then copy the ready-made JavaScript code.
Regex Cheat Sheet
| Token | Meaning | Token | Meaning |
|---|---|---|---|
. | Any character | \d \w \s | Digit, word char, whitespace |
* | 0 or more | \D \W \S | Their opposites |
+ | 1 or more | [abc] | One of a, b or c |
? | Optional, or lazy after a quantifier | [^abc] | Anything except a, b, c |
{2,5} | 2 to 5 times | ^ $ | Start / end |
(…) | Capturing group | \b | Word boundary |
(?:…) | Non-capturing group | a|b | a or b |
(?<name>…) | Named group | (?=…) (?!…) | Lookahead / negative |
Catastrophic Backtracking
Some patterns, especially nested quantifiers like (a+)+$ or (\w+\s?)*$, can take exponential time on inputs that almost match, freezing a browser tab or even taking down a server (a "ReDoS" attack). This tester runs every pattern in a separate Web Worker and stops it after 1.5 seconds, telling you what went wrong instead of hanging. To fix a slow pattern, remove nested quantifiers, make character classes more specific, and anchor patterns where possible.
Regex in Other Languages
Most syntax here, including classes, quantifiers, groups, anchors and lookahead, works the same in Python, Java, C#, PHP (PCRE), Ruby and Go. Differences to watch: Python uses (?P<name>…) for named groups; Go's RE2 has no lookarounds or backreferences; lookbehind support varies; and in many languages you must double every backslash inside string literals.
Tips for Writing Better Regular Expressions
- Start simple and build up, testing against both text that should match and text that shouldn't.
- Anchor your patterns with
^and$when validating whole inputs; otherwise "abc123" matches\d+. - Prefer specific classes:
[^"]*is faster and safer than.*inside quotes. - Use named groups to make patterns and replacements self-documenting.
- Use non-capturing groups
(?:…)when you only need grouping, not capturing. - Comment complex patterns in your code, or split them into smaller named pieces.
Useful Everyday Patterns
- Trim spaces at line ends:
[ \t]+$with the m flag, replaced with nothing. - Collapse multiple spaces:
{2,}replaced with a single space. - Find blank lines:
^\s*$with the m flag. - Swap "Last, First" to "First Last":
(\w+),\s*(\w+)replaced with$2 $1. - Extract hashtags:
#\w+with the g flag. - Match a whole word only: wrap it in
\b, e.g.\bcat\b, which won't match "category".
Try each pattern above in the tester with your own text to see exactly what it matches before using it in code or in your editor's find-and-replace.
Frequently Asked Questions
Why does my regex only find one match?
Turn on the g (global) flag to find all matches.
How do I match a literal dot or bracket?
Escape it with a backslash: \. \( \[. The String Escaper tool can escape any text for regex.
What's the difference between greedy and lazy?
Greedy quantifiers (*, +) match as much as possible. Adding ? (*?, +?) makes them match as little as possible.
Is there a perfect email regex?
No. Use a simple pattern for basic validation and confirm addresses by sending an email.
Is my text sent anywhere?
No. Everything runs in your browser.