Published May 18, 2025 ยท 4 min read ยท ๐Ÿท๏ธ Regex

Regular Expressions Cheat Sheet: 20 Patterns You Use 80% of the Time

Regex is powerful but notoriously hard to read. This guide covers the 20 most common patterns with copy-paste examples you can use immediately in your projects.

Basic Building Blocks

Before diving into patterns, understand these essentials:

  • . โ€” Any character except newline
  • \d โ€” Any digit (0-9)
  • \w โ€” Any word character (a-z, A-Z, 0-9, _)
  • \s โ€” Whitespace (space, tab, newline)
  • ^ โ€” Start of string
  • $ โ€” End of string
  • [abc] โ€” Any of a, b, or c
  • [^abc] โ€” NOT a, b, or c
  • | โ€” OR (alternation)
  • () โ€” Capture group
  • (?:) โ€” Non-capturing group

1. Email Address

const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

Validates the basic structure of most email addresses. For production, note that truly validating email requires sending a confirmation โ€” no regex is perfect.

2. URL

const urlRegex = /^https?:\/\/[^\s/$.?#].[^\s]*$/i;

Matches http and https URLs. The i flag makes it case-insensitive.

3. Phone Number (US Format)

const phoneRegex = /^\+?1?\s*\(?\d{3}\)?[\s.-]?\d{3}[\s.-]?\d{4}$/;

Matches formats like: +1 (555) 123-4567, 555-123-4567, 5551234567

4. Date (YYYY-MM-DD)

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

Validates ISO 8601 date format. This pattern also validates month and day ranges (won't accept Feb 31).

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

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

Matches 24-hour time format. Optional seconds.

6. Hex Color Code

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

Matches #RGB or #RRGGBB format. The # is optional.

7. IPv4 Address

const ipRegex = /^(?:(?:25[0-5]|2[0-4]\d|1?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|1?\d\d?)$/;

Matches valid IPv4 addresses from 0.0.0.0 to 255.255.255.255

8. Username (alphanumeric, 3-16 chars)

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

Letters, numbers, and underscores only. Length between 3 and 16.

9. Strong Password

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

Requires: lowercase, uppercase, digit, special char, minimum 8 characters. Uses lookahead assertions.

10. Credit Card Number (basic validation)

const ccRegex = /^(?:4[0-9]{12}|5[1-5][0-9]{14}|3[47][0-9]{13})$/;

Validates Visa, MasterCard, and Amex formats. Does not verify the checksum (Luhn algorithm).

Quick Reference: Quantifiers

  • * โ€” 0 or more
  • + โ€” 1 or more
  • ? โ€” 0 or 1
  • {n} โ€” Exactly n
  • {n,} โ€” n or more
  • {n,m} โ€” Between n and m

Try These Patterns Live

Test any of these patterns instantly in the Regex Tester tool. Paste your test string, enter the pattern, toggle flags (g, i, m), and see matches highlighted in real time.

โ†’ Try Regex Tester โ† Back to Blog