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.

Frequently Asked Questions

Why is my regex not matching what I expect?

Three usual suspects: (1) Missing anchors ^ and $ — /^\d+$/ requires the whole string to be digits, but /\d+/ matches 'abc123def'. (2) Greedy quantifiers — .* grabs too much; add ? for laziness. (3) Special characters unescaped — a literal dot needs \., a literal paren needs \(.

What's the difference between \b and \B?

\b matches a word boundary — the position between \w and \W (or string start/end). \B matches the opposite — anywhere a word boundary is NOT. Use \bword\b to match 'word' but not 'sword' or 'wordy'. \b doesn't consume characters; it's a zero-width assertion.

How do I make my regex faster?

Avoid catastrophic backtracking: nested quantifiers like (a+)+ are exponential. Use possessive quantifiers (a++) or atomic groups ((?>a+)) when supported. Prefer character classes over alternation: [aeiou] is faster than (a|e|i|o|u). Compile once if reusing: don't re-create the regex in a loop.

→ Try Regex Tester← Back to Blog
Copied!