Guide
Writing and testing regular expressions step by step
Regular expressions are compact and powerful, and easy to get subtly wrong. The reliable way to write one is against real sample text, one small piece at a time, watching what matches. This tester highlights matches as you type, lists every capture group, and previews replacements, all with the same JavaScript engine your web or Node.js code uses.
Testing a pattern
- Enter the pattern (without slashes) and the flags, or toggle flags with the chips.
- Paste test text: real data, including the edge cases you expect.
- Matches are highlighted, counted, and listed with their position and groups, including named groups.
- Try a replacement using
$1,$<name>or$&and copy the result.
If the pattern is invalid, the browser’s error message appears instead, such as an unterminated group or a nothing-to-repeat error.
Build it up gradually
Start with the most specific part, check it matches, then extend it. For an ISO date such as 2026-10-12:
\d{4}matches the year.\d{4}-\d{2}adds the month.(\d{4})-(\d{2})-(\d{2})captures all three parts as groups 1 to 3.(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})names them, so the replacement$<day>/$<month>/$<year>gives 12/10/2026.
At each step, the match list confirms you’re catching what you meant and nothing else.
Flags in one line each
| Flag | Effect |
|---|---|
g global |
Find all matches, not just the first |
i ignore case |
A matches a |
m multiline |
^ and $ match at every line, not just the whole text |
s dotAll |
. also matches line breaks |
u unicode |
Full Unicode, needed for emoji and \p{…} classes |
y sticky |
Match only at the current position (used in tokenizers) |
Patterns worth keeping
| Task | Pattern |
|---|---|
| Trailing spaces | [ \t]+$ with m |
| Repeated words (“the the”) | \b(\w+)\s+\1\b with i |
key=value pairs |
(\w+)=([^&\s]+) |
| Simple email shape (not full validation) | ^[^\s@]+@[^\s@]+\.[^\s@]+$ |
| Hex color | #(?:[0-9a-fA-F]{3}){1,2}\b |
| Text inside quotes | "([^"]*)" |
Common traps
- Greedy matching:
".*"on"a" and "b"matches the whole string. Use".*?"or"[^"]*". - Unescaped special characters:
.matches any character; write\.for a literal dot. The same goes for? + * ( ) [ ] { } ^ $ | \. - Forgetting anchors: without
^and$, a “validation” pattern accepts any text that merely contains a match. - Catastrophic backtracking: nested quantifiers like
(\w+\s?)+$can freeze on long, non-matching text. Make each part unambiguous. - Different engines: JavaScript, Python, PCRE (PHP), Java and .NET mostly agree, but advanced features differ. Re-test advanced patterns in the target language.
- Validating email or URLs perfectly: regex can check the shape, but real validation means sending a confirmation email or parsing with a URL library.
The cheat sheet
The panel at the bottom lists the tokens you’ll reach for most: \d, \w, \s, quantifiers like + * ? {2,4}, character sets, groups and named groups, alternation and lookaheads.
Without code
For one-off edits in a document, Find and Replace supports regular expressions too, and the Diff Checker shows exactly what a replacement changed.
Who tests regular expressions
- Developers write validation, parsing and search patterns.
- Data analysts extract values from text exports and logs.
- System administrators filter logs and configuration files.
- SEO and content specialists build redirect rules and analytics filters.
A complete example: extracting order numbers from emails
Order emails contain “Order #A-48213”:
- Paste a few sample emails into the test text.
- Type the pattern
Order #([A-Z]-\d{5})with the g flag. - The match list shows each order number in group 1.
- Add one email with an unusual format to check the pattern still matches only real order numbers.
Tips
- Test with examples that should not match as well.
- Keep patterns readable; a short comment in your code explains what they match.
Related tools
- Find and Replace to apply a pattern to a whole text.
- Diff Checker to compare text before and after.
- Text Cleaner for common cleanups without writing a pattern.
- Remove Duplicate Lines for de-duplicating lists.
Checked against the current version of the Regex Tester on .

