Regular Expression Builder
Build and test regular expressions with live explanations
๐ Regular Expression
/
/
Flags:
๐ Test Text
๐ฏ Match Results
0 matches
Enter a regex pattern and test text to see matches
Regular Expression Tester
Test and debug your regex patterns with detailed analysis
๐ Pattern Explanation
Enter a regex pattern to see detailed explanation
๐ป Code Examples
// Enter a regex pattern to see code examples
Common Regex Patterns
Ready-to-use regex patterns for common use cases
๐ง Email Address
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
Matches most valid email addresses
๐ Phone Number (US)
\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})
Matches US phone numbers in various formats
๐ URL/Website
https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)
Matches HTTP and HTTPS URLs
๐ IPv4 Address
^(?:(?: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]?)$
Matches valid IPv4 addresses
๐
Date (MM/DD/YYYY)
^(0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])[- /.](19|20)\d\d$
Matches dates in MM/DD/YYYY format
๐ Time (24-hour)
^([01]?[0-9]|2[0-3]):[0-5][0-9]$
Matches time in 24-hour format (HH:MM)
๐ณ Credit Card
^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|3[0-9]{13}|6(?:011|5[0-9]{2})[0-9]{12})$
Matches major credit card formats
๐จ Hex Color
^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$
Matches hex color codes (#RGB or #RRGGBB)
๐ Strong Password
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
8+ chars with uppercase, lowercase, digit, and special char
๐ค Username
^[a-zA-Z0-9_]{3,20}$
3-20 characters, letters, numbers, and underscores
๐ฎ ZIP Code (US)
^\d{5}(-\d{4})?$
Matches US ZIP codes (12345 or 12345-6789)
๐ Social Security Number
^\d{3}-?\d{2}-?\d{4}$
Matches SSN with or without dashes
Regex Cheat Sheet
Complete reference for regular expression syntax
๐ค Character Classes
.
Any character except newline
\d
Digit (0-9)
\D
Non-digit
\w
Word character (a-zA-Z0-9_)
\W
Non-word character
\s
Whitespace
\S
Non-whitespace
[abc]
Character set (a, b, or c)
[^abc]
Negated set (not a, b, or c)
[a-z]
Range (a through z)
๐ข Quantifiers
*
0 or more
+
1 or more
?
0 or 1 (optional)
{3}
Exactly 3
{3,}
3 or more
{3,5}
3 to 5
*?
0 or more (lazy)
+?
1 or more (lazy)
๐ Anchors
^
Start of string/line
$
End of string/line
\A
Start of string
\Z
End of string
\b
Word boundary
\B
Not word boundary
๐ฅ Groups
(abc)
Capture group
(?:abc)
Non-capture group
(?=abc)
Positive lookahead
(?!abc)
Negative lookahead
(?<=abc)
Positive lookbehind
(?
Negative lookbehind
๐ฏ Special Characters
|
Alternation (OR)
\
Escape character
\n
Newline
\r
Carriage return
\t
Tab
\0
Null character
๐ฉ Flags
g
Global (find all matches)
i
Case insensitive
m
Multiline (^ and $ match line breaks)
s
Dotall (. matches newlines)
u
Unicode
y
Sticky
Code Examples & Implementation
See how to use regex in different programming languages
๐ Implementation Examples
// JavaScript Regex Examples
// Basic matching
const regex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g;
const text = "Contact us at john@example.com or support@company.org";
const matches = text.match(regex);
console.log(matches); // ["john@example.com", "support@company.org"]
// Test if string matches
const emailRegex = /^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;
console.log(emailRegex.test("john@example.com")); // true
// Replace using regex
const phoneText = "Call me at (555) 123-4567 or (555) 987-6543";
const phoneRegex = /\((\d{3})\)\s(\d{3})-(\d{4})/g;
const formatted = phoneText.replace(phoneRegex, "$1-$2-$3");
console.log(formatted); // "Call me at 555-123-4567 or 555-987-6543"
// Extract with groups
const urlRegex = /https?:\/\/(www\.)?([a-zA-Z0-9.-]+)/;
const url = "Visit https://www.example.com for more info";
const match = url.match(urlRegex);
console.log(match[2]); // "example.com"
๐ ๏ธ Common Use Cases
โ
Form Validation
Validate user input in forms (email, phone, passwords)
๐ Data Extraction
Extract specific patterns from logs or text files
๐ Text Processing
Find and replace text patterns in documents
๐งน Data Cleaning
Clean and normalize data by removing unwanted characters
๐ Log Analysis
Parse log files and extract meaningful information
๐ Security
Detect malicious patterns in user input
๐ก Tips & Best Practices
- Start Simple: Begin with basic patterns and gradually add complexity
- Test Thoroughly: Always test your regex with various input examples
- Use Non-Capturing Groups: Use (?:...) when you don't need to capture the group
- Be Specific: Avoid overly broad patterns that match unintended text
- Escape Special Characters: Use backslash to escape meta characters
- Consider Performance: Complex regex can be slow on large texts
- Document Your Patterns: Add comments explaining complex regex patterns
- Use Online Tools: Tools like this one help visualize and test patterns