Common regex patterns for email, URL, phone, IP validation
✓Works with OpenClaudeYou are a regex pattern expert. The user wants to validate emails, URLs, phone numbers, and IP addresses using battle-tested regex patterns.
What to check first
- Confirm your regex engine supports standard PCRE or JavaScript RegExp syntax
- Decide if you need to validate format only or also check deliverability (regex can't verify live emails)
- Check if your target phone numbers have a specific country code requirement (US, international, etc.)
Steps
- Choose the validation type: email, URL, phone number, or IPv4/IPv6 address
- Select the regex pattern appropriate to your use case (strict validation vs. practical tolerance)
- Create a RegExp object with the
new RegExp()constructor or literal/pattern/syntax - Call
.test()to check if a string matches, or.exec()to extract matches - For email, know that RFC 5322 is overly complex—use a practical pattern instead
- For URLs, decide if you need protocol (
http://) or allow protocol-relative (//) - For phone numbers, handle country codes and formatting (spaces, dashes, parentheses)
- Test edge cases: empty strings, whitespace, special characters, boundary conditions
Code
// Email validation - practical RFC 5322 subset
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
// Stricter: /^[a-zA-Z0-9._%-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/
// URL validation - with optional protocol
const urlPattern = /^(https?:\/\/)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)$/;
// US Phone Number - flexible formatting
const usPhonePattern = /^(\+?1[-.\s]?)?\(?[2-9]\d{2}\)?[-.\s]?[2-9]\d{2}[-.\s]?\d{4}$/;
// International format: /^\+?[1-9]\d{1,14}$/
// IPv4 Address
const ipv4Pattern = /^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$/;
// IPv6 Address - simplified (full RFC 4291 is extremely complex)
const ipv6Pattern = /^(([0-9a-fA-F]{1,4}:){7}[0-9a-fA
Note: this example was truncated in the source. See the GitHub repo for the latest full version.
Common Pitfalls
- Treating this skill as a one-shot solution — most workflows need iteration and verification
- Skipping the verification steps — you don't know it worked until you measure
- Applying this skill without understanding the underlying problem — read the related docs first
When NOT to Use This Skill
- When a simpler manual approach would take less than 10 minutes
- On critical production systems without testing in staging first
- When you don't have permission or authorization to make these changes
How to Verify It Worked
- Run the verification steps documented above
- Compare the output against your expected baseline
- Check logs for any warnings or errors — silent failures are the worst kind
Production Considerations
- Test in staging before deploying to production
- Have a rollback plan — every change should be reversible
- Monitor the affected systems for at least 24 hours after the change
Related Code Generation Skills
Other Claude Code skills in the same category — free to download.
Type Generator
Generate TypeScript types from JSON/API responses
Interface from JSON
Generate interfaces from JSON samples
Enum Generator
Generate enums from constant values
Boilerplate Reducer
Generate boilerplate code patterns
Regex Builder
Build and test regular expressions
SQL Generator
Generate SQL queries from natural language
Mock Data Generator
Generate realistic mock data (Faker.js)
Type Guard Generator
Generate TypeScript type guards
Want a Code Generation skill personalized to YOUR project?
This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.