Developer
Regular Expressions — A Practical Guide for Developers
Learn regular expressions from scratch. Syntax, common patterns, flags, and practical examples with our free regex tester.
I avoided regex for years. Every time I needed to validate an email or parse a log line, I'd write 20 lines of string manipulation code instead of learning the syntax. Then I got paired with a senior dev who wrote ^\b\w+@\w+\.\w+\b$ in about 3 seconds and moved on. That was the day I decided to actually learn regex.
What Is a Regular Expression?
A regular expression is a pattern that describes a set of strings. You use it to:
- Search for specific text patterns
- Validate input (emails, phone numbers, etc.)
- Replace text programmatically
- Extract data from unstructured text
Basic Syntax
Literal Characters
/hello/ matches "hello"
/123/ matches "123"
Special Characters
| Pattern | Matches | Example |
|---|---|---|
| . | Any character (except newline) | h.t → "hat", "hot" |
| \d | Any digit (0-9) | \d{3} → "123" |
| \w | Word character (a-z, A-Z, 0-9, _) | \w+ → "hello_world" |
| \s | Whitespace | \s+ → " " |
| ^ | Start of string | ^hello |
| $ | End of string | world$ |
| * | 0 or more | a* → "", "a", "aaa" |
| + | 1 or more | a+ → "a", "aaa" |
| ? | 0 or 1 | colou?r → "color", "colour" |
| {n} | Exactly n times | \d{4} → "2025" |
| {n,m} | Between n and m times | \d{2,4} → "12", "1234" |
Common Patterns
Email Validation
URL Matching
Phone Number (US)
IP Address
Regex Flags
| Flag | Meaning | Example |
|---|---|---|
| g | Global (all matches) | /hello/g |
| i | Ignore case | /hello/i |
| m | Multiline | /^hello/m |
| s | Dot matches newline | /./s |
| u | Unicode support | /\p{Emoji}/u |
Groups and Capturing
Use parentheses to capture parts of a match:
/(\d{4})-(\d{2})-(\d{2})/
// Matches "2025-05-10", captures year, month, day
When NOT to Use Regex
- Parsing HTML/XML — Use a proper parser instead
- Complex nested structures — Regex can't handle recursion well
- Very large files — Performance issues with massive text
Test Your Regex
Use our free regex tester to test patterns in real-time with highlighting, flags, and match details.