Common Regex Patterns
- \d - Matches any digit (0-9)
- \w - Matches any word character (letters, digits, underscore)
- \s - Matches any whitespace character (space, tab, newline)
- . - Matches any character except newline
- ^ - Matches the start of a line
- $ - Matches the end of a line
Quantifiers
- * - Matches 0 or more times
- + - Matches 1 or more times
- ? - Matches 0 or 1 time
- {n} - Matches exactly n times
- {n,} - Matches n or more times
- {n,m} - Matches between n and m times
Useful Examples
- Email:
\b[\w.%+-]+@[\w.-]+\.[A-Za-z]{2,}\b - URL:
https?://[\w.-]+\.[\w]{2,}[^\s]* - Phone (US):
\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4} - Hex Color:
#[0-9A-Fa-f]{6}\b - Date (YYYY-MM-DD):
\d{4}-\d{2}-\d{2}
Flags Explained
g (global): Find all matches rather than stopping after the first match.
i (case-insensitive): Ignore case when matching letters (A = a).
m (multiline): ^ and $ match the start/end of lines, not just the string.
s (dotAll): Dot (.) matches newline characters as well.