Regex Testing and Debugging: From Basics to Efficient Patterns
Text(Updated Apr 23, 2026)
Why Test Regex Online?
Regular expressions are powerful but easy to get wrong on the first try. An online tester helps you:
- Highlight matches in real time: See what matches as you type
- Visualize capture groups: Inspect each group clearly
- Toggle flags quickly: Switch
g/i/m/swithout redeploying - Catch syntax errors instantly: Invalid patterns are flagged immediately
Regex Syntax Cheat Sheet
Basic matching
| Syntax | Meaning | Example | Matches |
|---|---|---|---|
. |
Any character (except newline) | a.c |
abc, a1c |
\d |
Digit | \d{3} |
123 |
\w |
Word character | \w+ |
hello_1 |
\s |
Whitespace | a\sb |
a b |
[abc] |
Character class | [aeiou] |
a, e, i |
[^abc] |
Negated class | [^0-9] |
non-digits |
Quantifiers
| Syntax | Meaning | Greedy / Lazy |
|---|---|---|
* |
Zero or more | a* / a*? |
+ |
One or more | a+ / a+? |
? |
Zero or one | a? / a?? |
{n,m} |
Between n and m | a{2,4} |
Groups and references
| Syntax | Meaning | Example |
|---|---|---|
(abc) |
Capturing group | (\d{4})-(\d{2}) |
(?:abc) |
Non-capturing group | (?:http|https):// |
\1 |
Backreference | (\w)\1 matches aa, bb |
(?<name>) |
Named group | (?<year>\d{4}) |
Using the Online Regex Tester
Steps
- Open the Regex Tester
- Enter your pattern in the regex field
- Paste sample text in the test field
- Choose flags (
gglobal /icase-insensitive /mmultiline) - Review highlights and match details
Common flags
| Flag | Meaning | Default |
|---|---|---|
g |
Find all matches | First match only |
i |
Ignore case | Case-sensitive |
m |
Multiline mode | Single-line |
s |
. matches newlines |
. does not match newlines |
u |
Unicode mode | — |
Common Pitfalls
1. Greedy matching
Text: <div>hello</div><div>world</div>
Pattern: <div>.*</div>
Result: entire string matched as one block!
Fix: <div>.*?</div> ← lazy match, two separate matches
2. Catastrophic backtracking
// Dangerous: nested quantifiers cause exponential backtracking
/^(a+)+$/.test('a'.repeat(30) + 'b');
// This test can take seconds or hang
// Fix: avoid nested quantifiers, or use atomic groups where supported
3. Forgetting to escape special characters
Pattern: price.com ← . matches any character
Fix: price\.com ← matches price.com only
Practical Examples
| Use case | Pattern |
|---|---|
| US phone (simplified) | ^\+?1?\d{10,11}$ |
| Email (basic) | ^[\w.-]+@[\w-]+\.[\w.]+$ |
| URL | ^https?:\/\/[\w.-]+(?:\.[\w]+)+[/\w.-]*$ |
| IP address | ^(\d{1,3}\.){3}\d{1,3}$ |
| Date YYYY-MM-DD | `^\d{4}-(?:0[1-9] |
| CJK characters | [\u4e00-\u9fff]+ |
FAQ
Pattern works in the tester but not in code?
Check whether you forgot the g flag, or whether your string contains newlines but the pattern lacks m / s.
How do I match multiline text?
Use the s flag so . matches newlines, or use [\s\S] instead of ..
Summary
Regex is essential for developers, but the syntax is easy to get wrong. Use the Regex Tester to validate and debug in real time. ToolsKu also offers Find & Replace and Text Diff for a complete text workflow.
#正则表达式#Regex#文本处理#教程#调试