How to Test Regex (Regular Expressions) Online
Master Regular Expressions with our complete guide to testing regex patterns. Learn syntax, flags, and debugging techniques with our free online Regex Tester.
Regular Expressions (Regex) are one of the most powerful tools in a developer’s arsenal. They allow you to search, match, and replace complex text patterns with a single line of code.
But with great power comes great confusion. Regex syntax is notoriously dense and difficult to read. A single misplaced character can break your pattern or, worse, create a “catastrophic backtracking” loop that crashes your server.
This is why you should always test your regex before deploying it. Our Free Regex Tester makes this easy.
How Regex Testing Works
Testing regex involves three steps:
- Define the Pattern: What are you looking for? (e.g., an email address).
- Set the Flags: How should the search behave? (Case-insensitive? Global?).
- Provide Test Data: A sample string that includes both matches and non-matches (edge cases).
Common Syntax Cheat Sheet
| Symbol | Meaning | Example |
|---|---|---|
^ | Start of string/line | ^Hello matches “Hello” at start |
$ | End of string/line | World$ matches “World” at end |
. | Any character (except newline) | a.c matches “abc”, “a@c” |
* | 0 or more occurrences | a* matches "", “a”, “aaaa” |
+ | 1 or more occurrences | a+ matches “a”, “aaaa” |
? | 0 or 1 occurrence (optional) | colou?r matches “color”, “colour” |
\d | Digit (0-9) | \d{3} matches “123” |
\w | Word character (a-z, 0-9, _) | \w+ matches “user_name” |
[...] | Character set | [aeiou] matches any vowel |
Debugging with Visual Highlighting
The hardest part of regex is knowing what matched and why.
Did (.*) match the whole line or just part of it? Did your capture group (\d+) grab the area code or the whole phone number?
Our Regex Tester highlights matches in yellow and breaks down every capture group ( $1, $2 ) so you can see exactly how the engine is parsing your text.
Common Patterns Worth Memorizing
A few regex patterns come up repeatedly across projects. Keeping these on hand saves time:
- Email (basic):
^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$matches standard email addresses. - Phone (US):
^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$handles formats like (555) 123-4567, 555-123-4567, and 555.123.4567. - URL:
https?://[^\s/$.?#].[^\s]*captures HTTP and HTTPS links in running text. - IPv4 Address:
\b\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\bmatches IP-like strings (add range validation in code). - Date (YYYY-MM-DD):
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])enforces valid month and day ranges at the format level.
Test each pattern against both valid and intentionally invalid inputs to confirm boundary behavior.
Lookaheads and Lookbehinds
Lookaheads and lookbehinds let you assert that certain text appears before or after your match without including it in the result.
- Positive lookahead
(?=...)matches only if followed by the given pattern. For example,\d+(?= dollars)matches “50” in “50 dollars” but not “50” in “50 euros”. - Negative lookahead
(?!...)matches only if NOT followed by the pattern.\d+(?! dollars)matches “50” in “50 euros” but skips “50” in “50 dollars”. - Positive lookbehind
(?<=...)works the same way in reverse.(?<=\$)\d+matches “99” in “$99” but not in “99 items”. - Negative lookbehind
(?<!...)excludes matches preceded by a pattern.
Lookarounds are powerful for extracting data from structured text without capturing delimiters, and they keep replacement operations cleaner since the surrounding context stays untouched.
Debugging Tips for Tricky Patterns
When a regex doesn’t behave as expected, try these approaches:
- Simplify first. Strip the pattern down to its core match, verify it works, then add quantifiers and groups one at a time.
- Check greedy vs. lazy.
.*grabs as much text as possible (greedy). Adding?makes it lazy:.*?grabs as little as possible. This distinction causes most “matched too much” bugs. - Watch for catastrophic backtracking. Nested quantifiers like
(a+)+can cause the engine to try exponentially many paths on non-matching input. Rewrite nested quantifiers with atomic groups or possessive quantifiers where your engine supports them. - Use multiline and dotall flags intentionally. The
mflag makes^and$match line boundaries instead of string boundaries. Thesflag lets.match newline characters. Forgetting these flags is a common source of partial matches. - Log intermediate capture groups. If your full pattern fails, check each
(...)group individually to see which sub-pattern is misbehaving.
Best Practices
- Be Specific:
.*is lazy. Avoid using “match anything” wildcards if you can define the structure (e.g.,\d+or[a-z]+). - Escape Special Characters: If you want to match a literal dot
.or question mark?, you must escape it with a backslash:\.\?. - Test Edge Cases: Does your email regex catch
user+tag@domain.co.uk? Does your date regex reject2024-02-30(since Feb 30 doesn’t exist, though simple regex can’t validate calendar logic)? - Anchor Your Patterns: Use
^and$to prevent partial matches when validating full strings like emails or phone numbers. - Comment Complex Patterns: In languages that support verbose mode (
xflag), add inline comments to explain each section of a long regex.
Regex Flags Explained
Flags modify how the regex engine processes your pattern. Applying the wrong flag (or forgetting one) is a frequent source of bugs.
| Flag | Name | Effect |
|---|---|---|
g | Global | Find all matches, not just the first |
i | Case-insensitive | A and a are treated as identical |
m | Multiline | ^ and $ match line boundaries instead of string boundaries |
s | Dotall | . matches newline characters |
u | Unicode | Enables full Unicode support for \p{} property escapes |
y | Sticky | Matches only from the lastIndex position |
Common mistake: Forgetting the g flag when using replace() in JavaScript. Without it, only the first match is replaced:
'aaa'.replace(/a/, 'b'); // 'baa' (only first match)
'aaa'.replace(/a/g, 'b'); // 'bbb' (all matches)
When to Avoid Regex
Regex is powerful, but it isn’t always the right tool:
- Parsing HTML or XML. Regular expressions can’t handle nested structures. Use a proper parser (DOMParser, cheerio, BeautifulSoup) instead.
- Complex validation with business rules. An email regex can check format, but it can’t verify that the mailbox actually exists. Use a validation library or API.
- Readable code. A regex like
^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$is correct for password validation, but a series of simpleifchecks is far more maintainable.
When regex is the right tool, test it thoroughly before deploying. Open the Regex Tester and paste your pattern now.
Frequently Asked Questions
What is the difference between greedy and lazy matching?
Greedy quantifiers (*, +, {n,m}) match as much text as possible. Lazy quantifiers (*?, +?, {n,m}?) match as little as possible. For example, given the input <b>bold</b>, the greedy pattern <.*> matches the entire string <b>bold</b>, while the lazy pattern <.*?> matches just <b>. Use lazy matching when you want the shortest possible match.
Why does my regex work in one language but not another?
Regex implementations differ between languages. JavaScript doesn’t support lookbehinds in older engines (pre-ES2018). Python’s re module uses a different syntax for named groups ((?P<name>...)) than JavaScript ((?<name>...)). POSIX regex (used in grep and sed) doesn’t support \d or \w shortcuts. Always test your pattern in the specific language you plan to use.
How do I match a literal backslash in regex?
Use a double backslash: \\. In most programming languages, you also need to escape the backslash in the string literal itself, so the final code often looks like \\\\ in languages like Java. In JavaScript, using a regex literal (/\\/) avoids the extra escaping layer.
Can regex match across multiple lines?
By default, . doesn’t match newline characters and ^/$ match only the start and end of the entire string. Enable the s (dotall) flag to make . match newlines. Enable the m (multiline) flag to make ^ and $ match at line boundaries. These flags can be combined as needed.
Related Calculators
Related Articles
- Color Theory Basics for Web Design
Master color theory for web design including the color wheel, complementary palettes, WCAG contrast requirements, and color psychology to create effective interfaces.
- How to Minify CSS for Faster Websites
Learn what CSS minification does, why it speeds up your website, what it removes from your stylesheets, and best practices for minifying CSS in production.
- Cron Job Examples for Common Tasks (Copy-Paste Ready)
Practical cron job examples with clear explanations. Copy-paste ready crontab schedules for backups, reports, cleanup, monitoring, and automation tasks.
- Common JSON Syntax Errors and How to Fix Them
Fix JSON syntax errors fast with this developer guide. Learn the top 5 JSON parsing errors, before/after examples, and debugging techniques to validate JSON instantly.
Share this article
Have suggestions for this article?