hank-builds.com

Common Regex Patterns

Ready-to-use patterns for everyday tasks. Click "Try it" to open in the regex tester with sample data.

Common

Email Address

Matches most common email address formats.

/[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/g
Test: Contact us at info@example.com or support@company.co.uk

Matches HTTP and HTTPS URLs.

/https?://[\w.-]+(?:\.[a-z]{2,})(?:/[\w./?%&=-]*)?/gi
Test: Visit https://example.com/path?q=1 or http://test.org

Phone Number (US)

Matches US phone numbers in various formats.

/(?:\+?1[-.\s]?)?\(?\d{3}\)?[-.\s]?\d{3}[-.\s]?\d{4}/g
Test: Call (555) 123-4567 or +1-555-987-6543 or 5551234567

Matches UUID/GUID format.

/[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/gi
Test: ID: 550e8400-e29b-41d4-a716-446655440000

File Extension

Extracts file extension from a filename.

/\.(\w+)$/gm
Test: index.html style.css app.tsx README

Networking

IPv4 Address

Matches valid IPv4 addresses (0.0.0.0 to 255.255.255.255).

/\b(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\b/g
Test: Server IPs: 192.168.1.1, 10.0.0.255, 999.999.999.999 (invalid)

Dates & Times

Date (YYYY-MM-DD)

Matches dates in ISO 8601 format.

/\d{4}-(?:0[1-9]|1[0-2])-(?:0[1-9]|[12]\d|3[01])/g
Test: Released on 2024-01-15, updated 2024-12-31

Time (HH:MM)

Matches 24-hour time format.

/(?:[01]\d|2[0-3]):[0-5]\d/g
Test: Meeting at 09:30, lunch at 12:00, done by 17:45

Web

Hex Color

Matches 3 or 6 digit hex color codes.

/#(?:[0-9a-fA-F]{3}){1,2}\b/g
Test: Colors: #fff, #3b82f6, #F0F, #1a2b3c

HTML Tag

Matches paired HTML tags with content.

/<([a-z][a-z0-9]*)\b[^>]*>.*?</\1>/gi
Test: <div class="test">Hello</div> <p>World</p>

Markdown Link

Matches Markdown-style links [text](url).

/\[([^\]]+)\]\(([^)]+)\)/g
Test: Check [Google](https://google.com) and [GitHub](https://github.com)

CSS Property

Matches CSS property-value pairs.

/([a-z-]+)\s*:\s*([^;]+);/g
Test: color: red; background-color: #fff; font-size: 16px;

Validation

Password Strength

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

/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[!@#$%^&*]).{8,}$/gm
Test: weak Strong1! P@ssw0rd! hello GoodP@ss1

Text Processing

Whitespace Trimming

Matches leading and trailing whitespace.

/^\s+|\s+$/gm
Test: hello world trim me

Duplicate Words

Finds consecutive duplicate words.

/\b(\w+)\s+\1\b/gi
Test: The the quick brown fox fox jumped over the the lazy dog

Quoted String

Matches single or double quoted strings with escape support.

/(["'])(?:(?!\1|\\).|\\.)*\1/g
Test: Say "hello world" and 'it\'s ok'

Camel to Snake Case

Matches camelCase boundaries. Replace with $1_$2 and lowercase.

/([a-z])([A-Z])/g
Test: myVariableName getSomeValue HTMLParser

Numbers

Positive Integer

Matches positive integers (no leading zeros, no negatives).

/(?<![-\d])[1-9]\d*\b/g
Test: Values: 0, 1, 42, 007, 100, -5

Decimal Number

Matches decimal numbers including negative.

/-?\d+\.\d+/g
Test: Pi is 3.14159, temperature -12.5, size 0.5

Code

Import Statement

Matches JavaScript/TypeScript import statements.

/import\s+(?:\{[^}]+\}|\w+)\s+from\s+['"]([^'"]+)['"]/g
Test: import { useState } from 'react' import App from './App'