Regex Reference Guide

Regex Cheat Sheet: Common Patterns Explained

Regular expressions (regex) are incredibly powerful tools for matching, parsing, and validating text input. However, their syntax is notoriously dense and hard to parse at a glance. Having a quick-reference guide for standard search patterns can save developers hours of trial and error. Below is a cheat sheet of the five most common regular expressions used in modern web applications.

1Email Validation

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

What it matches: Validates the general structure of email addresses. It ensures characters are present before the @ symbol, followed by a valid domain host, and a top-level domain (TLD) extension that is at least 2 letters long.

Matches: user.name@example.com, hello_world123@quietbench.dev
Fails: user@example, @domain.com

2URL Matching

/^https?:\/\/(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)$/

What it matches: Matches standard web URLs beginning with either http:// or https://. It supports optional subdomains, paths, parameters, anchors, and queries.

Matches: https://quietbench.dev/regex-tester?q=active, http://www.google.com/
Fails: ftp://files.example.com, quietbench.dev (no protocol)

3US Phone Number

/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/

What it matches: Validates 10-digit telephone numbers in standard US formats. It handles an optional 3-digit area code enclosed in parentheses, followed by digits separated by dashes, dots, spaces, or nothing at all.

Matches: (555) 123-4567, 555-123-4567, 5551234567
Fails: 555-12-34, +1-555-123-45678

4IPv4 Address

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

What it matches: Matches IP addresses in dotted-decimal format. The expression verifies that all four segments (octets) of the address contain numbers falling strictly within the allowed range of 0 to 255.

Matches: 192.168.1.100, 127.0.0.1
Fails: 256.0.0.1 (above range), 192.168.1 (incomplete)

5Hex Color Code

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

What it matches: Validates HTML/CSS hex color codes. The expression matches an optional leading hash # followed by either a 6-digit hex representation (two digits each for red, green, and blue) or a shorthand 3-digit version.

Matches: #3b82f6, #FFF, 000
Fails: #GGGGGG (invalid letters), #FF12 (invalid length)
Getting a syntax error instead? Fix "Invalid Regular Expression"

Need to test or write a custom regex pattern?

Test regular expressions with live highlighting, test suites, and plain-English explanations.

Test your expressions instantly with our free Regex Tester