Regular expressions (regex) are one of the most powerful tools in a developer's arsenal — and one of the most feared. A well-crafted regex can extract, validate, or transform text in a single line. A poorly written one can create bugs that haunt your codebase for years.
This guide will teach you regex from practical examples, not abstract theory. We'll cover the most useful patterns, common pitfalls, and how to test your expressions in real-time using our free Regex Tester — which runs entirely in your browser with no data leaving your device.
Regex Basics: The Building Blocks
Literal Characters
The simplest regex is just text. The pattern hello matches the exact string "hello". But regex becomes powerful when you add special characters.
Character Classes
. → any character (except newline)
\d → any digit [0-9]
\w → any word character [a-zA-Z0-9_]
\s → any whitespace (space, tab, newline)
\D → any non-digit
\W → any non-word character
\S → any non-whitespaceQuantifiers
* → zero or more
+ → one or more
? → zero or one (optional)
{3} → exactly 3
{2,5} → between 2 and 5
{3,} → 3 or moreAnchors
^ → start of string (or line with /m flag)
$ → end of string (or line with /m flag)
\b → word boundaryGroups and Alternation
(abc) → capturing group
(?:abc) → non-capturing group
a|b → alternation (a or b)
(?=abc) → positive lookahead
(?!abc) → negative lookahead10 Regex Patterns Every Developer Should Know
1. Email Validation (Basic)
^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$Matches most common email formats. Note: true email validation per RFC 5322 is extremely complex. For production use, send a verification email instead.
2. URL Matching
https?:\/\/[\w.-]+(?:\.[\w]{2,})(?:\/[\w./?#&=-]*)?3. IPv4 Address
\b(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b4. Date (YYYY-MM-DD)
^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$5. Phone Number (International)
^\+?[1-9]\d{1,14}$6. Hex Color Code
^#(?:[0-9a-fA-F]{3}){1,2}$7. Password Strength (Minimum Requirements)
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$Requires: lowercase, uppercase, digit, special character, minimum 8 characters.
8. HTML Tag Extraction
<([a-z][a-z0-9]*)\b[^>]*>(.*?)<\/\1>Caveat: regex is not a proper HTML parser. Use this for simple extraction only. For real HTML manipulation, use a DOM parser.
9. Whitespace Cleanup
\s{2,}Matches two or more consecutive whitespace characters. Replace with a single space to normalize.
10. CSV Value Extraction
(?:^|,)("(?:[^"]*(?:""[^"]*)*)"|[^,]*)How to Test Regex: A Workflow
- Start with a specific example. Write the exact string you want to match before writing any regex.
- Build incrementally. Start with the simplest pattern that matches, then add complexity. Don't try to write the whole regex at once.
- Test with edge cases. Try empty strings, very long strings, strings with special characters, and strings that should NOT match.
- Use a visual tester. Our Regex Tester highlights matches in real-time as you type, showing capture groups and match positions.
- Check performance. Regex with nested quantifiers can cause catastrophic backtracking. If a regex takes more than a few milliseconds, simplify it.
Common Regex Mistakes
1. Greedy vs Lazy Matching
// Greedy (default): matches as much as possible
"<div>hello</div><div>world</div>".match(/<div>.*<\/div>/);
// Matches: "<div>hello</div><div>world</div>" (the whole thing!)
// Lazy (add ?): matches as little as possible
"<div>hello</div><div>world</div>".match(/<div>.*?<\/div>/);
// Matches: "<div>hello</div>" (just the first one)2. Forgetting to Escape Special Characters
These characters have special meaning and must be escaped with \ to match literally: . * + ? ^ $ { } [ ] ( ) | \
3. Catastrophic Backtracking
// ❌ Dangerous — can hang on long inputs
/(a+)+b/
// ✅ Safe equivalent
/a+b/Nested quantifiers (like (a+)+) create exponential backtracking. Always test your regex with long inputs that don't match.
4. Not Anchoring Patterns
Without ^ and $ anchors, your pattern might match a substring when you intended to match the whole string:
// Matches "abc" inside "xabcx" — probably not what you want
/abc/
// Only matches if the entire string is "abc"
/^abc$/Regex in Different Languages
JavaScript
const pattern = /\d{3}-\d{3}-\d{4}/g;
const matches = text.match(pattern);
const replaced = text.replace(pattern, "***-***-****");
// Named groups (ES2018+)
const datePattern = /(?<year>\d{4})-(?<month>\d{2})-(?<day>\d{2})/;
const { groups } = "2026-03-20".match(datePattern);
// groups.year === "2026"Python
import re
pattern = r'\d{3}-\d{3}-\d{4}'
matches = re.findall(pattern, text)
replaced = re.sub(pattern, '***-***-****', text)
# Named groups
date_match = re.match(r'(?P<year>\d{4})-(?P<month>\d{2})-(?P<day>\d{2})', '2026-03-20')
year = date_match.group('year') # "2026"Command Line (grep/sed)
# Find lines matching a pattern
grep -E '\d{3}-\d{3}-\d{4}' file.txt
# Replace in-place
sed -i 's/old-pattern/new-value/g' file.txt
# Extract matches only
grep -oE '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}' file.txtTest Your Regex Now
Our Regex Tester gives you a live playground to build and debug patterns. Paste your test string, type your regex, and see matches highlighted instantly. The tool supports all JavaScript regex flags and shows capture group details.
Everything runs in your browser — your data never leaves your device. This makes it safe to test patterns against sensitive data like log files, user data, or API responses.
For a quick reference, check our Regex Cheat Sheet with searchable, copy-to-clipboard examples.
Summary
Regex is a skill that pays off enormously once you master the basics. Start simple, build incrementally, and always test with edge cases. Avoid nested quantifiers, remember to anchor your patterns, and use lazy matching when extracting from markup.
The DevToolbox Regex Tester is your free, private sandbox for building and debugging regular expressions — try it now.