Regex Tester
Test and debug regular expressions in real-time. See matches highlighted instantly with detailed match information.
You May Also Like
What is Regex?
Regular expressions (regex) are patterns used to match character combinations in text. Think of them as a super-powered "Find" feature.
While Ctrl+F finds exact text, regex finds patterns:
- All email addresses in a document
- Phone numbers in any format
- Dates regardless of format
- Words that start with "un-"
Every programmer eventually needs regex. It's a skill that pays dividends forever.
Basic Syntax
Literal Characters
Most characters match themselves: hello matches "hello"
Special Characters (Metacharacters)
| Character | Meaning | Example |
|---|---|---|
. | Any single character | h.t matches "hat", "hit", "hot" |
* | Zero or more of previous | ab*c matches "ac", "abc", "abbc" |
+ | One or more of previous | ab+c matches "abc", "abbc" (not "ac") |
? | Zero or one of previous | colou?r matches "color", "colour" |
^ | Start of string/line | ^Hello matches "Hello world" |
$ | End of string/line | world$ matches "Hello world" |
To match a special character literally, escape it with backslash: \. matches an actual period.
Character Classes
Character classes match any ONE character from a set:
| Pattern | Matches | Example |
|---|---|---|
[abc] | a, b, or c | [cb]at → "cat", "bat" |
[a-z] | Any lowercase letter | [a-z]+ → "hello" |
[A-Z] | Any uppercase letter | [A-Z][a-z]+ → "Hello" |
[0-9] | Any digit | [0-9]{3} → "123" |
[^abc] | NOT a, b, or c | [^0-9]+ → "hello" |
Shorthand Classes
| Shorthand | Equivalent | Meaning |
|---|---|---|
\d | [0-9] | Digit |
\D | [^0-9] | Non-digit |
\w | [a-zA-Z0-9_] | Word character |
\W | [^a-zA-Z0-9_] | Non-word character |
\s | [ \t\n\r\f] | Whitespace |
\S | [^ \t\n\r\f] | Non-whitespace |
Quantifiers
Quantifiers specify how many times a pattern should repeat:
| Quantifier | Meaning | Example |
|---|---|---|
* | 0 or more | a* → "", "a", "aaa" |
+ | 1 or more | a+ → "a", "aaa" (not "") |
? | 0 or 1 | a? → "", "a" |
{3} | Exactly 3 | a{3} → "aaa" |
{2,4} | 2 to 4 | a{2,4} → "aa", "aaa", "aaaa" |
{2,} | 2 or more | a{2,} → "aa", "aaa", ... |
By default, quantifiers are greedy — they match as much as possible. Add ? to make them lazy: .*? matches as little as possible.
Groups and Alternation
Parentheses create groups:
(abc)+ # Matches "abc", "abcabc", etc.
(Mr|Mrs|Ms) # Matches "Mr", "Mrs", or "Ms"
Capturing groups remember what they matched:
(\d{3})-(\d{4})
This captures area code and number separately.
Non-capturing groups for organization only:
(?:Mr|Mrs|Ms)\. \w+
Common Regex Patterns
📧 Email (simplified)
[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}
📱 US Phone Number
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}
🔗 URL
https?://[\w\-]+(\.[\w\-]+)+[\w\-.,@?^=%&:/~+#]*
📅 Date (YYYY-MM-DD)
\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])
🎨 Hex Color
#(?:[0-9a-fA-F]{3}){1,2}\b
Regex Tips & Best Practices
1. Start Simple Build your regex piece by piece. Test each addition.
2. Be Specific When Possible
[0-9] is clearer than \d for readability. \d{4} is better than \d+ when you know the length.
3. Use Anchors
^ and $ prevent partial matches. ^\d{5}$ matches "12345" but not "123456".
4. Test Edge Cases
- Empty strings
- Very long inputs
- Special characters
- Unicode characters
5. Comment Complex Patterns Use verbose mode or document your regex:
// Format: (area)-(prefix)-(line)
const phone = /\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}/;
6. Know When NOT to Use Regex
- Parsing HTML/XML (use a parser)
- Complex nested structures
- When simple string methods work