RegEx Tester
Test, debug, and visualize Regular Expressions with real-time feedback and detailed match information.
Test Text
Chars: 0Match Results
Matches: 0No matches yet. Enter a regex pattern and test text, then click "Test RegEx".
RegEx Visualization
RegEx Cheat Sheet
Common RegEx Examples
What is a Regular Expression (RegEx)?
A Regular Expression (often abbreviated as RegEx) is a powerful sequence of characters that forms a search pattern. It's used for pattern matching within strings, allowing developers to perform complex text searches, validations, extractions, and replacements with incredible precision and efficiency.
Think of regular expressions as a supercharged "Find" function that can match patterns rather than exact text. They're essential tools for text processing, data validation, log analysis, and many other programming tasks.
RegEx Visualization Example
^(\d{3})-(\d{3})-(\d{4})$RegEx Components Explained
| Category | Pattern | Description | Example |
|---|---|---|---|
| Anchors | ^, $ |
Match start/end of string or line | ^Hello matches "Hello" at string start |
| Character Classes | \d, \w, \s |
Digits, word chars, whitespace | \d{3} matches "123" |
| Quantifiers | *, +, ?, {n} |
Specify how many times to match | \d+ matches one or more digits |
| Groups | (), (?:), (?=) |
Capture or non-capture groups | (\d{3}) captures area code |
| Alternation | | |
Match either pattern | cat|dog matches "cat" or "dog" |
| Character Sets | [abc], [^abc], [a-z] |
Match any character in set | [aeiou] matches any vowel |
| Escape Sequences | \., \\, \+ |
Match special characters literally | \.com matches ".com" literally |
Practical RegEx Use Cases
1. Email Validation
// Basic email validation
/^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,}$/
// Explanation:
// ^ - Start of string
// [A-Za-z0-9._%+-]+ - One or more valid characters before @
// @ - Literal @ symbol
// [A-Za-z0-9.-]+ - Domain name (one or more chars)
// \. - Literal dot
// [A-Z|a-z]{2,} - TLD (2+ letters)
// $ - End of string
2. URL Extraction
// Extract URLs from text
/https?:\/\/[^\s/$.?#].[^\s]*/gi
// Explanation:
// https? - http or https
// :\/\/ - Literal ://
// [^\s/$.?#] - Any character except whitespace, /, $, ., ?, #
// . - Any character
// [^\s]* - Zero or more non-whitespace characters
// gi - Global and case-insensitive flags
3. Phone Number Formatting
// Match various phone formats
/\(?(\d{3})\)?[-.\s]?(\d{3})[-.\s]?(\d{4})/
// Matches:
// (123) 456-7890
// 123-456-7890
// 123.456.7890
// 123 456 7890
// Groups captured:
// Group 1: Area code (123)
// Group 2: Prefix (456)
// Group 3: Line number (7890)
4. Password Strength Check
// At least 8 chars, with uppercase, lowercase, number, special char
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
// Explanation:
// ^ - Start of string
// (?=.*[a-z]) - At least one lowercase letter (lookahead)
// (?=.*[A-Z]) - At least one uppercase letter (lookahead)
// (?=.*\d) - At least one digit (lookahead)
// (?=.*[@$!%*?&])- At least one special character (lookahead)
// [A-Za-z\d@$!%*?&]{8,} - 8 or more of allowed characters
// $ - End of string
RegEx Flags Explained
- g (Global): Find all matches, not just the first one
- i (Case-insensitive): Match both uppercase and lowercase
- m (Multiline): ^ and $ match start/end of each line, not just whole string
- s (Dot All): . matches newline characters as well
- u (Unicode): Treat pattern as Unicode code points
- y (Sticky): Match only from the index indicated by lastIndex property
Performance Tips
- Use Specific Character Classes:
\dis faster than[0-9] - Avoid Greedy Quantifiers: Use
*?instead of*when possible - Compile Complex Patterns: For repeated use, create a RegExp object
- Be Careful with Backtracking: Complex patterns can cause exponential slowdown
- Use Anchors:
^and$help engine optimize matching - Test with Real Data: Ensure your regex performs well with expected inputs
Frequently Asked Questions (FAQ)
What's the difference between .* and .*?
.* is greedy - it matches as much as possible while still allowing the rest of the pattern to match. .*? is lazy - it matches as little as possible while still allowing the rest of the pattern to match. For example, in "abc123def", /.*\d/ matches "abc123" while /.*?\d/ matches "abc1".
How do I match special characters literally?
Use a backslash \ to escape special characters. For example, to match a literal dot: \., to match a literal backslash: \\, to match a literal plus sign: \+. The special characters that need escaping are: .\*+?^${}()|[]\/
What are lookaheads and lookbehinds?
Lookaheads and lookbehinds are zero-width assertions that don't consume characters in the string. (?=...) is a positive lookahead (asserts that what follows matches), (?!...) is a negative lookahead (asserts that what follows doesn't match). Lookbehinds (?<=...) and (?<!...) work similarly but look backward.
How can I make my regex more readable?
Use the x flag (if supported) to ignore whitespace and allow comments. Break complex patterns into multiple lines with comments. Use named capture groups (?<name>...) for better clarity. Test and visualize your regex to understand how it works.
Why isn't my regex matching what I expect?
Common issues: 1) Forgetting that regex is case-sensitive by default (use i flag), 2) Not escaping special characters, 3) Using . when you mean to match any character including newline (use [\s\S] or s flag), 4) Greedy quantifiers matching too much, 5) Not considering multiline input (use m flag for ^ and $).