DEVELOPER GUIDE · 11 min read

REGEX PATTERNS EVERY DEVELOPER SHOULD KNOW

20 essential patterns for validation, extraction, and text processing — ready to copy and use.

By Jobin Blancaflor·February 10, 2026·11 min read

TL;DR: Bookmark this page. These 20 regex patterns cover 90% of real-world validation and parsing needs. Test all of them live with our free Regex Tester — no install, real-time match highlighting.

Validation Patterns

Email Address

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

Validates the common structure of an email address. Note: no regex fully validates email per RFC 5322 — the spec allows edge cases no one uses in practice. This pattern catches 99.9% of real-world email addresses while rejecting obvious garbage. Always send a confirmation email for definitive validation.

URL (HTTP/HTTPS)

/^https?:\/\/[\w\-]+(\.[\w\-]+)+([\w\-.,@?^=%&:/~+#]*[\w\-@?^=%&/~+#])?$/

Matches http:// and https:// URLs including paths, query strings, and fragments. Rejects bare domain names and other protocols.

IP Address (IPv4)

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

Strictly validates IPv4 addresses including the 0–255 range constraint per octet. Rejects invalid values like 999.0.0.1.

Phone Number (International)

/^\+?[1-9]\d{1,14}$/

Validates E.164 international phone number format. Allows an optional leading +, then 2–15 digits. For country-specific formats, you'll need format-specific patterns.

Date (YYYY-MM-DD)

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

Validates ISO 8601 date format. Checks month range (01–12) and day range (01–31). Does not validate day/month combinations — use a date library for full validation.

Credit Card Number

/^(?:4[0-9]{12}(?:[0-9]{3})?|5[1-5][0-9]{14}|3[47][0-9]{13}|6(?:011|5[0-9]{2})[0-9]{12})$/

Matches Visa (starts with 4), Mastercard (51–55), Amex (34 or 37), and Discover (6011 or 65). Always run the Luhn algorithm in addition for real payment validation.

UK Postcode

/^[A-Z]{1,2}[0-9][0-9A-Z]?\s?[0-9][A-Z]{2}$/i

Covers all valid UK postcode formats including single-letter, double-letter, and the various district/sector patterns.

Extraction Patterns

Extract All URLs from Text

/https?:\/\/[^\s<>"']+/g

Finds all HTTP/HTTPS URLs within a larger block of text. Use with the global flag to get all matches.

Extract All Emails from Text

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

Harvests all email addresses from a document, log file, or data dump.

Extract HEX Colours

/#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})\b/g

Finds all HEX colour codes in CSS files, design tokens, or HTML. Useful for colour audits and palette extraction.

Semantic Version Numbers

/\b(\d+)\.(\d+)\.(\d+)(?:-[\w.]+)?(?:\+[\w.]+)?\b/g

Matches semver versions like 1.2.3, 2.0.0-beta.1, and 3.1.4+build.5. Capture groups give you major, minor, patch.

Security Patterns

Password Strength (Minimum Requirements)

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

Uses lookaheads to require at least one lowercase letter, one uppercase letter, one digit, and one special character, minimum 8 characters. Adjust the special character set and length to your policy.

Basic SQL Injection Detection

/('|\-\-|\/\*|\*\/|;|\bOR\b|\bAND\b|\bUNION\b|\bSELECT\b|\bDROP\b)/i

A defensive filter to flag potential SQL injection attempts in user input. This is a supplement to parameterised queries, not a replacement.

JWT Token Structure

/^[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]+\.[A-Za-z0-9\-_]*$/

Validates the three-part base64url structure of a JWT (header.payload.signature). Use our JWT Decoder to inspect the actual contents.

Text Processing Patterns

Normalise Whitespace

/\s+/g  →  replace with ' '

Collapses multiple spaces, tabs, and newlines into a single space. Essential for cleaning user input before storage.

Strip HTML Tags

/<[^>]*>/g  →  replace with ''

Removes all HTML tags from a string. Useful for extracting plain text from HTML fragments. For serious sanitisation, use a dedicated library — regex alone cannot safely sanitise HTML.

camelCase to kebab-case

/([a-z])([A-Z])/g  →  replace with '$1-$2', then .toLowerCase()

Converts backgroundColor to background-color. Useful for CSS property name conversion.

Generate URL Slug

str.toLowerCase().replace(/[^\w\s-]/g, '').replace(/[\s_-]+/g, '-').replace(/^-+|-+$/g, '')

Converts "How to Format JSON Online!" to "how-to-format-json-online". Three replacements: strip non-word characters, collapse whitespace/underscores/hyphens to a single hyphen, trim leading/trailing hyphens.

Extract Markdown Headings

/^#{1,6}\s+(.+)$/gm

Extracts all heading text from a Markdown document. Use with the multiline flag so ^ matches line starts.

Test Your Patterns Live

Regex patterns are notoriously difficult to get right without testing. Edge cases, backtracking, flag interactions, and character class nuances all create surprises. Our Regex Tester lets you write a pattern, enter test strings, and see every match highlighted in real time — including capture group contents.

TEST REGEXES LIVE

Real-time match highlighting, capture groups, all flags supported.

Open Regex Tester →

RELATED ARTICLES