20 Regex Patterns Every Developer Should Know

A curated collection of the most useful regular expressions — from email validation to hex colors, with explanations you can actually understand.

20 Regex Patterns Every Developer Should Know

The Regex Reference You Actually Need

Rather than another tutorial, this is a practical reference of patterns you'll actually use — with clear explanations of how each part works.

1. Email Address

/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/

Validates standard email format. Not RFC-compliant (there is no perfect email regex), but catches typos and common mistakes.

2. URL

/^https?:\/\/[\w\-]+(\.[\w\-]+)+[/#?=$]*/

Matches http and https URLs with a hostname and optional path.

3. IPv4 Address

/^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}
  (?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/

Validates each octet as 0–255, preventing invalid IPs like 256.0.0.1.

4. Phone Number (US)

/^\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}$/

Matches (555) 123-4567, 555-123-4567, 5551234567, etc.

5. Hex Color Code

/^#?([a-fA-F0-9]{6}|[a-fA-F0-9]{3})$/

Matches #FFF, #ffffff, #000, with or without the # prefix.

6. Date (YYYY-MM-DD)

/^\d{4}-(0[1-9]|1[0-2])-(0[1-9]|[12]\d|3[01])$/

Validates year, month (01–12), and day (01–31) with proper month bounds.

7. Time (HH:MM or HH:MM:SS)

/^([01]\d|2[0-3]):([0-5]\d)(:[0-5]\d)?$/

Matches 00:00 to 23:59, with optional seconds.

8. Username

/^[a-zA-Z0-9_-]{3,16}$/

Alphanumeric + underscore/hyphen, 3–16 characters.

9. Strong Password

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)
  (?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$/

Requires: lowercase, uppercase, digit, special char, min 8 chars.

10. Credit Card (basic)

/^\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}$/

Matches Visa, Mastercard format. Use a payment library for real validation.

11. Slug / URL-friendly string

/^[a-z0-9]+(?:-[a-z0-9]+)*$/

Lowercase letters, numbers, single hyphens between words. No leading/trailing hyphens.

12. HTML Tag

/^<([a-z]+)([^<]+)*(?:>(.*)<\/\1>|\s+\/>)$/

Matches opening tags with content and closing tags, or self-closing tags.

13. Comma-Separated Numbers

/^\d{1,3}(,\d{3})*$/

Matches 1,000, 1,000,000 but not 10,00 or 1000,000.

14. UUID

/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-
  [0-9a-f]{4}-[0-9a-f]{12}$/i

Matches standard UUID format (v4 or any version).

15. File Extension

/\.(jpg|jpeg|png|gif|webp)$/i

Extracts image file extensions from filenames.

Test Your Regexes

Use our Regex Tester to validate patterns against test strings — with real-time highlighting of matches and capture groups.

← Back to Blog
Copied!