Common Real-World Patterns
Combine all the concepts you've learned to build patterns for common real-world use cases.· 10 min
Concept
Real-world regex patterns combine anchors, character classes, quantifiers, and groups. The key is to be specific about what varies and what is fixed.
Some common patterns:
**Email (simplified):**
[\w.+\-]+@[\w\-]+\.[\w.\-]+
This is a simplified pattern that covers common email formats. It does not fully conform to RFC 5322 — for example, it does not handle quoted local parts or IP address domains. For most form validation, a simplified pattern plus a confirmation email is the recommended approach.
**US Phone:**
\(?\d{3}\)?[\s\-.]?\d{3}[\s\-.]?\d{4}
**Hex color:**
#[0-9a-fA-F]{6}(?:[0-9a-fA-F]{2})?
Building patterns step by step — fixed text first, then variable parts — makes complex patterns approachable. Remember that no single regex can validate every edge case — use them for common-case filtering and handle exceptions in code.
/[\w.+\-]+@[\w\-]+\.[a-zA-Z]{2,}/gA simplified email pattern: local-part @ domain . tld
/#[0-9a-fA-F]{6}/gMatches a CSS hex color code
Exercise
Write a pattern matching a US ZIP code: either 5 digits alone, or 5 digits + dash + 4 digits (ZIP+4 format).
Your pattern:
Must match
Must not match
Try These Patterns
See these concepts in action with real-world patterns from the library: