Regex Tester & Debugger: The Complete Guide
What is a Regular Expression?
A regular expression (regex or regexp) is a sequence of characters that defines a search pattern. It is a powerful tool for pattern matching, text extraction, validation, and manipulation. Nearly every programming language supports regex in some form (JavaScript, Python, Java, PHP, Go, Rust, etc.), making it an essential skill for any developer.
The Regex Tester & Debugger provides a real-time environment where you type a pattern and a test string, and instantly see matches highlighted with group capture details. This immediate feedback makes it the fastest way to learn, build, and debug regex patterns.
Regex Syntax Basics
A regex pattern is composed of literal characters and metacharacters. Literal characters match themselves, while metacharacters have special meaning.
Literal Matching
The simplest regex is a literal string:
Pattern: hello
Text: "Say hello to everyone"
Match: hello (at position 4)
Metacharacters
| Metacharacter | Meaning | Example |
|---|---|---|
. | Any single character (except newline) | h.t matches "hat", "hot", "hut" |
^ | Start of string | ^hello matches "hello" at the start |
$ | End of string | world$ matches "world" at the end |
* | Zero or more of the preceding element | ab*c matches "ac", "abc", "abbc" |
+ | One or more of the preceding element | ab+c matches "abc", "abbc" but not "ac" |
? | Zero or one of the preceding element | colou?r matches "color" and "colour" |
| | Alternation (OR) | cat|dog matches "cat" or "dog" |
\ | Escape character | \. matches a literal dot |
Tip: The Regex Tester highlights matches in real-time as you type. Experiment with these metacharacters to see how each one behaves.
Regex Flags
Flags modify how the regex engine searches. The tool supports six flags, each togglable with a single click:
| Flag | Name | Effect |
|---|---|---|
g | Global | Find all matches, not just the first one |
i | Case-insensitive | Match uppercase and lowercase interchangeably |
m | Multiline | ^ and $ match line boundaries |
s | Dotall | Make . match newline characters |
u | Unicode | Enable full Unicode matching |
y | Sticky | Match only from the last match position |
Pattern: hello
Flags: i (case-insensitive)
Text: "Hello, HELLO, hello"
Match: Hello, HELLO, hello (all three)
Pattern: ^hello
Flags: m (multiline)
Text: "goodbye\nhello\nworld"
Match: hello (on the second line)
Character Classes & Quantifiers
Predefined Character Classes
| Class | Matches | Equivalent To |
|---|---|---|
\d | Any digit | [0-9] |
\w | Any word character (letter, digit, underscore) | [a-zA-Z0-9_] |
\s | Any whitespace (space, tab, newline) | [ \t\n\r\f] |
\D | Any non-digit | [^0-9] |
\W | Any non-word character | [^a-zA-Z0-9_] |
\S | Any non-whitespace | [^ \t\n\r\f] |
Custom Character Sets
[aeiou] -- Matches any vowel
[a-z] -- Matches any lowercase letter
[0-9] -- Matches any digit
[^abc] -- Matches anything except a, b, or c
[0-9a-f] -- Matches any hexadecimal digit
Quantifiers in Detail
| Quantifier | Meaning | Example |
|---|---|---|
{n} | Exactly n times | \d{3} matches exactly 3 digits |
{n,} | n or more times | \d{2,} matches 2 or more digits |
{n,m} | Between n and m times | \d{2,4} matches 2 to 4 digits |
+ | One or more | \d+ matches one or more digits |
* | Zero or more | \d* matches zero or more digits |
? | Zero or one | \d? matches an optional digit |
Groups & Capture
Parentheses () serve two purposes in regex: grouping (to apply quantifiers) and capturing (to extract sub-matches).
Capturing Groups
Each pair of parentheses creates a capturing group numbered from left to right:
Pattern: (\d{3})-(\d{3})-(\d{4})
Text: "Call 555-123-4567 today"
Match: 555-123-4567
Group 1: 555
Group 2: 123
Group 3: 4567
The Regex Tester displays each capture group alongside its match in the match list, making it easy to verify that your groups capture the correct substrings.
Non-Capturing Groups
Use (?:...) when you need grouping without capture:
Pattern: (?:https?:\/\/)?([\w.-]+)
Text: "https://example.com"
Match: https://example.com
Group 1: example.com
Named Capture Groups
Modern regex engines support named groups using the (?<name>...) syntax.
Lookahead & Lookbehind
Lookaround assertions let you match a pattern only if it is followed or preceded by another pattern, without including the lookaround text in the match.
| Assertion | Syntax | Example |
|---|---|---|
| Positive lookahead | (?=...) | \d(?=px) matches a digit before "px" |
| Negative lookahead | (?!...) | \d(?!px) matches a digit not before "px" |
| Positive lookbehind | (?<=...) | (?<=\$)\d+ matches digits after $ |
| Negative lookbehind | (?<!...) | (?<!\$)\d+ matches digits not after $ |
Pattern: \w+(?=@) -- Match word characters before "@"
Text: "user@example.com"
Match: user
Pattern: (?<=https?:\/\/)[\w.-]+ -- Match domain after "http://"
Text: "Visit https://example.com"
Match: example.com
Common Regex Patterns
Here are patterns you will use frequently in real-world development:
| Purpose | Pattern | Matches |
|---|---|---|
[\w.-]+@[\w.-]+\.\w{2,} | user@example.com | |
| URL | https?://[\w./?-]+ | https://example.com |
| US Phone | \d{3}-\d{3}-\d{4} | 555-123-4567 |
| Date (ISO) | \d{4}-\d{2}-\d{2} | 2026-06-17 |
| IP Address | \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} | 192.168.1.1 |
| Hex Color | #[0-9a-fA-F]{6} | #ff6600 |
| Whitespace trim | ^\s+|\s+$ | Leading/trailing spaces |
Tip: The Regex Tester shows match count, character position, and group details in real-time. It is the fastest way to validate and debug any regex pattern.
Practical Examples
Validating Email Addresses
A common task is validating email input on a form:
Pattern: ^[\w.-]+@[\w.-]+\.\w{2,}$
"alice@example.com" -- Match
"bob@gmail.com" -- Match
"not-an-email" -- No match
"user@.com" -- No match
Extracting Data from Logs
Server logs often contain structured data that regex can extract:
Log line: "ERROR 2026-06-17 14:30:00 [auth] Failed login for user alice"
Pattern: ^(\w+)\s+(\d{4}-\d{2}-\d{2}\s+\d{2}:\d{2}:\d{2})\s+\[(\w+)\]\s+(.+)$
Group 1: ERROR
Group 2: 2026-06-17 14:30:00
Group 3: auth
Group 4: Failed login for user alice
Password Validation
Enforcing password rules with a single pattern:
Pattern: ^(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[!@#$%]).{8,}$
Must have: uppercase, lowercase, digit, special char, min 8 chars
Replacing Text in Code
Use regex in your editor to refactor code. For example, renaming a function call:
Find: oldFunction\(([^)]+)\)
Replace: newFunction()
Before: oldFunction(userId)
After: newFunction(userId)
Regex Best Practices
- Start simple: Build your regex incrementally. Start with the literal pattern, add flexibility, then add anchors and groups.
- Test edge cases: Always test with empty strings, very long strings, strings with special characters, and strings that almost match.
- Use anchors: Use
^and$to prevent partial matches when validating entire strings. - Prefer non-capturing groups: Use
(?:...)when you only need grouping, not capture. It is faster and keeps match lists clean. - Be careful with greedy quantifiers:
.*can match too much. Use.*?(lazy) when you want the smallest match. - Comment complex patterns: Use the
xflag (if supported) or break your pattern across lines with comments. - Test in the Regex Tester: Always validate your regex in the Regex Tester before using it in production code.
Ready to test and debug your regular expressions?
Try Regex Tester & Debugger