Regex Tester Complete Guide 2026 โ Master Regular Expressions
Regular expressions (regex) are powerful tools for pattern matching and text manipulation. This comprehensive guide teaches regex from basics to advanced techniques, with practical examples and a free online regex tester.
Want to test regex patterns now? Use our free Regex Tester to validate and debug regular expressions in real-time โ no signup required.
What is Regular Expression (Regex)?
A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. Regex is used for:
- Validation: Check if input matches expected format (email, phone, credit card)
- Search & Replace: Find and modify text matching patterns
- Extraction: Pull specific data from larger text blocks
- Parsing: Analyze structured text (logs, config files)
Regex in Action
// Email validation regex
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
// Matches: [email protected]
// Does NOT match: invalid.email@
Basic Regex Syntax
Literal Characters
Most characters match themselves:
Special Characters (Metacharacters)
These characters have special meaning in regex:
. ^ $ * + ? { } [ ] \ | ( )
Character Classes
| Pattern | Matches | Example |
|---|---|---|
[abc] |
Any one character: a, b, or c | /[aeiou]/ matches any vowel |
[^abc] |
Any character EXCEPT a, b, c | /[^0-9]/ matches non-digits |
[a-z] |
Range: any lowercase letter | /[A-Z]/ matches uppercase |
\d |
Any digit (0-9) | /\d\d\d/ matches "123" |
\w |
Word character (a-z, A-Z, 0-9, _) | /\w+/ matches "hello_world" |
\s |
Whitespace (space, tab, newline) | /\s+/ matches multiple spaces |
. |
Any character except newline | /a.b/ matches "axb", "a b", "a1b" |
Quantifiers: How Many to Match
Quantifiers specify how many times a character or group should match:
| Quantifier | Meaning | Example | Matches |
|---|---|---|---|
* |
0 or more times | /ab*c/ |
"ac", "abc", "abbc", "abbbc" |
+ |
1 or more times | /ab+c/ |
"abc", "abbc" (NOT "ac") |
? |
0 or 1 time (optional) | /colou?r/ |
"color", "colour" |
{3} |
Exactly 3 times | /\d{3}/ |
"123", "456" |
{2,4} |
2 to 4 times | /a{2,4}/ |
"aa", "aaa", "aaaa" |
{3,} |
3 or more times | /a{3,}/ |
"aaa", "aaaa", "aaaaa" |
Greedy vs Lazy Quantifiers
// Greedy (matches as much as possible)
/<.+>/ matches "hello" as ONE match
// Lazy (matches as little as possible)
/<.+?>/ matches "" and "" separately
Anchors and Boundaries
Anchors don't match characters โ they match positions:
| Anchor | Matches | Example |
|---|---|---|
^ |
Start of string/line | /^Hello/ โ "Hello" at start |
$ |
End of string/line | /world$/ โ "world" at end |
\b |
Word boundary | /\bcat\b/ โ "cat" as whole word |
\B |
Non-word boundary | /\Bcat\B/ โ "cat" inside word |
Examples
/\.com$/ โ Matches strings ending with .com
/\b\d{3}\b/ โ Matches standalone 3-digit numbers
Groups and Capturing
Capturing Groups
Parentheses () create groups that can be referenced later:
// Match date format: YYYY-MM-DD
/(\d{4})-(\d{2})-(\d{2})/
Group 1: Year (2026)
Group 2: Month (03)
Group 3: Day (10)
Non-Capturing Groups
Use (?:) for grouping without capturing:
// Match "http" or "https" without capturing
/https?:\/\/(?:www\.)?example\.com/
Backreferences
Reference captured groups within the regex:
// Match repeated words
/\b(\w+)\s+\1\b/
Matches: "hello hello", "test test"
Does NOT match: "hello world"
Named Groups
// JavaScript named groups
/(?\d{4})-(?\d{2})-(?\d{2})/
// Access: match.groups.year, match.groups.month
Lookarounds: Advanced Assertions
Lookarounds check if a pattern exists without including it in the match:
| Assertion | Syntax | Description |
|---|---|---|
| Positive Lookahead | (?=pattern) |
Match only if followed by pattern |
| Negative Lookahead | (?!pattern) |
Match only if NOT followed by pattern |
| Positive Lookbehind | (?<=pattern) |
Match only if preceded by pattern |
| Negative Lookbehind | (?<!pattern) |
Match only if NOT preceded by pattern |
Practical Examples
// Match dollar amount (only number, not $)
/(?<=\$)\d+(\.\d{2})?/
Matches "99.99" in "$99.99"
// Match filename without extension
/\w+(?=\.txt)/
Matches "document" in "document.txt"
// Match not followed by comma
/\d+(?!,)/
Matches "123" in "123" but not in "123,"
Common Regex Patterns (Cheat Sheet)
Email Validation
// Basic email regex
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
// Note: Perfect email validation is complex; use libraries for production
Phone Numbers
// US phone: (123) 456-7890 or 123-456-7890
/^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/
// International (basic)
/^\+?\d{1,3}[-.\s]?\(?\d{1,4}\)?[-.\s]?\d{1,4}[-.\s]?\d{1,9}$/
URL Matching
// HTTP/HTTPS URLs
/^https?:\/\/(?:www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b(?:[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)$/
IP Addresses
// IPv4
/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/
// IPv6 (simplified)
/^([0-9a-fA-F]{1,4}:){7}[0-9a-fA-F]{1,4}$/
Credit Card Numbers
// Basic card number (13-19 digits)
/^\d{13,19}$/
// With optional spaces/dashes
/^\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}$/
Password Strength
// 8+ chars, 1 uppercase, 1 lowercase, 1 digit, 1 special
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
Regex Flags (Modifiers)
Flags change how regex matching works:
| Flag | Name | Effect |
|---|---|---|
i |
Case Insensitive | /hello/i matches "HELLO", "Hello", "hello" |
g |
Global | Find all matches, not just the first |
m |
Multiline | ^ and $ match line starts/ends, not just string |
s |
DotAll | . matches newlines (not just non-newline chars) |
u |
Unicode | Enable full Unicode support |
y |
Sticky | Match only at current position ( lastIndex ) |
Combining Flags
/hello/gi // Global + case insensitive
/^.+$/gm // Multiline: ^ and $ per line, global
How to Test Regex Patterns
Method 1: Online Regex Tester
- Enter your regex pattern
- Input test string
- See matches highlighted in real-time
- Adjust pattern until it works
Method 2: JavaScript Console
// Test if pattern matches
const regex = /\d{3}-\d{4}/;
regex.test("123-4567"); // true
// Get all matches
const text = "Call 123-4567 or 987-6543";
text.match(/\d{3}-\d{4}/g); // ["123-4567", "987-6543"]
// Get capture groups
const match = "2026-03-10".match(/(\d{4})-(\d{2})-(\d{2})/);
console.log(match[1]); // 2026 (year)
Method 3: Python
import re
# Search
result = re.search(r'\d{3}-\d{4}', 'Call 123-4567')
print(result.group()) # 123-4567
# Find all
re.findall(r'\d+', 'a1b2c3') # ['1', '2', '3']
# Replace
re.sub(r'\d+', 'NUM', 'a1b2c3') # 'aNUMbNUMcNUM'
Regex Performance Tips
Avoid Catastrophic Backtracking
// BAD: Can cause exponential time on certain inputs
/(a+)+$/
// GOOD: Use possessive quantifiers or atomic groups
/(?:a+)$/
Be Specific
// Slow: matches too much, then backtracks
/.*.*<\/title>.*/
// Fast: specific about what can appear where
/[^<]*[^<]*<\/title>[^<]*/
Use Anchors
// Slower: searches entire string
/\d{3}-\d{4}/
// Faster: anchors prevent unnecessary searching
/^\d{3}-\d{4}$/
Character Classes vs Dot
// Less efficient
/.*@.*\..*/
// More efficient (specific about what can appear)
/[a-z]+@[a-z]+\.[a-z]+/
Regex by Programming Language
JavaScript
const regex = /^[a-z]+$/i;
regex.test('Hello'); // true
// Replace
'Hello World'.replace(/\s+/g, '-'); // "Hello-World"
// Exec with groups
const match = /(?\w+) (?\w+)/.exec('John Doe');
console.log(match.groups.first); // "John"
Python
import re
pattern = r'^[a-z]+$'
re.match(pattern, 'hello', re.IGNORECASE)
# Find all
re.findall(r'\d+', 'a1b2c3')
# Named groups
m = re.match(r'(?P\w+) (?P\w+)', 'John Doe')
print(m.group('first')) # John
PHP
// Pattern delimiter is /
preg_match('/^\d{3}-\d{4}$/', '123-4567'); // 1 (match)
// Find all
preg_match_all('/\d+/', 'a1b2c3', $matches);
// Replace
preg_replace('/\s+/', '-', 'Hello World'); // "Hello-World"
Java
import java.util.regex.*;
Pattern p = Pattern.compile("^\\d{3}-\\d{4}$");
Matcher m = p.matcher("123-4567");
boolean found = m.matches(); // true
Common Regex Mistakes
โ Forgetting to Escape Special Characters
// WRONG: . matches ANY character
/Hello.World/ matches "HelloxWorld", "Hello World", "Hello9World"
// CORRECT: Escape the dot
/Hello\.World/ matches only "Hello.World"
โ Greedy Matching Surprises
// Text: "abc123def456"
/.*\d/ // Matches entire string (greedy to last digit)
/.*?\d/ // Matches "abc1" (lazy, stops at first digit)
โ Not Anchoring When Needed
// Want to validate entire input
/^\d{3}-\d{4}$/ // Correct: full match required
/\d{3}-\d{4}/ // Wrong: matches partial strings
โ Over-Engineering
// Overly complex email regex (200+ chars)
// vs simple validation
/^[^@\s]+@[^@\s]+\.[^@\s]+$/ // Good enough for basic check
Regex Tools and Resources
Online Tools
- DevKits Regex Tester โ Free, real-time testing
- Regex101 โ Detailed explanations, multiple flavors
- RegExr โ Interactive, community patterns
- Regex Storm โ .NET regex tester
Learning Resources
- Regular-Expressions.info โ Comprehensive tutorials
- RexEgg โ Advanced regex techniques
- RegexOne โ Interactive exercises
Pattern Libraries
- RegexLib โ Community regex patterns
- AnyPixelLab Regex Library โ Common validations
Conclusion
Regular expressions are indispensable tools for developers. Master regex fundamentals, practice with real examples, and use online testers to debug complex patterns.
Key Takeaways:
- Start simple, add complexity gradually
- Use character classes for specificity
- Anchor patterns when validating input
- Test with edge cases (empty strings, special chars)
- Use flags appropriately (i, g, m, s)
- Profile regex performance on large inputs
๐ Test Your Regex Now
Use our free Regex Tester to validate and debug regular expressions in real-time. Live highlighting, match groups, and replace testing.
Test Regex