Regular Expressions (Regex) Patterns Guide for Developers
Master regex patterns with practical examples for email validation, URL parsing, data extraction, and text processing. Learn regex syntax, common patterns, and best practices.
What are Regular Expressions?
Regular expressions (regex or regexp) are patterns used to match character combinations in strings. They're incredibly powerful for searching, validating, and manipulating text data.
Basic Regex Syntax
Literal Characters
Most characters match themselves literally:
Pattern: cat Matches: "cat" in "The cat sat" Does not match: "Cat" or "cats"Metacharacters
Special characters with specific meanings:
- . (dot): Matches any single character except newline
- * (asterisk): Matches 0 or more of the preceding element
- + (plus): Matches 1 or more of the preceding element
- ? (question mark): Matches 0 or 1 of the preceding element
- ^ (caret): Matches the start of a string
- $ (dollar): Matches the end of a string
Character Classes
Common Character Classes
[abc] → Matches any single character a, b, or c [a-z] → Matches any lowercase letter [A-Z] → Matches any uppercase letter [0-9] → Matches any digit [a-zA-Z] → Matches any letter \\d → Matches any digit (same as [0-9]) \\w → Matches any word character (letters, digits, underscore) \\s → Matches any whitespace character \\D → Matches any non-digit \\W → Matches any non-word character \\S → Matches any non-whitespace characterQuantifiers
Quantifiers specify how many times a pattern should match:
{n} → Exactly n times
{n,} → At least n times
{n,m} → Between n and m times
Examples:
\d{3} → Matches exactly 3 digits: "123"
\d{2,4} → Matches 2 to 4 digits: "12", "123", "1234"
[a-z]{1,} → Matches 1 or more lowercase lettersCommon Regex Patterns
1. Email Validation
// Basic email pattern
/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
Matches: user@example.com, john.doe@company.co.uk
Does not match: invalid@, @example.com, user@.com2. URL Validation
// URL pattern with protocol
/^https?:\/\/(www\.)?[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}(\/[^\s]*)?$/
Matches: https://example.com, http://www.site.com/page
Does not match: example.com, ftp://example.com3. Phone Number Validation
// US phone number
/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/
Matches: (123) 456-7890, 123-456-7890, 123.456.7890
Does not match: 12-345-6789, 123-45-678904. Date Validation (MM/DD/YYYY)
// Basic date pattern
/^(0[1-9]|1[0-2])\/(0[1-9]|[12][0-9]|3[01])\/([0-9]{4})$/
Matches: 01/15/2024, 12/31/2023
Does not match: 13/01/2024, 01/32/2024, 1/1/245. Password Strength
// At least 8 chars, 1 uppercase, 1 lowercase, 1 digit, 1 special char
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/
Matches: Password1!, SecureP@ss9
Does not match: password, Password1, short1!Advanced Regex Techniques
Groups and Capturing
Parentheses create groups that can be referenced later:
// Extract parts of a phone number
const pattern = /\((\d{3})\)\s*(\d{3})-(\d{4})/;
const match = "(123) 456-7890".match(pattern);
console.log(match[1]); // "123" (area code)
console.log(match[2]); // "456" (prefix)
console.log(match[3]); // "7890" (line number)Lookahead and Lookbehind
Assert what comes before or after without including it in the match:
// Positive lookahead (?=...)
/\d+(?= dollars)/
Matches: "100" in "100 dollars" but not in "100 euros"
// Negative lookahead (?!...)
/\d+(?! dollars)/
Matches: "100" in "100 euros" but not in "100 dollars"
// Positive lookbehind (?<=...)
/(?<=\$)\d+/
Matches: "100" in "$100" but not in "€100"Practical Applications
Data Extraction from Text
// Extract all email addresses from text
const text = "Contact us at: support@example.com or sales@example.com";
const emailPattern = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g;
const emails = text.match(emailPattern);
// Result: ["support@example.com", "sales@example.com"]String Replacement
// Replace phone numbers with masked version
const text = "Call me at 123-456-7890";
const masked = text.replace(
/(\d{3})-(\d{3})-(\d{4})/,
"XXX-XXX-$3"
);
// Result: "Call me at XXX-XXX-7890"Input Validation
// Validate username: 3-16 chars, alphanumeric and underscore
function validateUsername(username) {
const pattern = /^[a-zA-Z0-9_]{3,16}$/;
return pattern.test(username);
}
validateUsername("john_doe"); // true
validateUsername("ab"); // false (too short)
validateUsername("user@123"); // false (invalid char)Best Practices
- Keep it simple: Complex regex is hard to maintain and debug
- Use comments: Document complex patterns with explanation
- Test thoroughly: Use multiple test cases including edge cases
- Consider performance: Avoid catastrophic backtracking with nested quantifiers
- Use raw strings: In most languages, use raw strings to avoid escape issues
- Validate on both client and server: Never trust client-side validation alone
Common Pitfalls
- Greedy vs lazy matching: Use ? after quantifiers for lazy matching
- Forgetting to escape metacharacters: Use \\ to match literal special characters
- Not anchoring patterns: Use ^ and $ to match entire strings
- Over-validating: Don't make patterns too strict (e.g., accepting international formats)
Try Our String Tools
Practice regex and string manipulation with our developer tools: