markshares/Regex Cookbook

Regex Cookbook

Click any pattern to copy. Use Try in playground to test live.

Playground

/
gFind all matches (not just the first)iCase-insensitive — A matches am^ and $ match start/end of each linesDot (.) matches newline characters too

Validation

Email address

/i
^[^\s@]+@[^\s@]+\.[^\s@]+$copy

Matches a basic email address.

  • user@example.com
  • name+tag@domain.co
  • not-an-email
  • @nodomain.com

URL (http/https)

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

Matches http and https URLs.

  • https://example.com
  • http://sub.domain.co/path?q=1#hash
  • ftp://nope.com
  • example.com

US phone number

^\+?1?[\s.\-]?\(?\d{3}\)?[\s.\-]?\d{3}[\s.\-]?\d{4}$copy

Matches common US phone formats.

  • (555) 867-5309
  • 555.867.5309
  • +1 555 867 5309
  • 867-5309

US ZIP code

^\d{5}(-\d{4})?$copy

Matches 5-digit and ZIP+4 formats.

  • 22801
  • 22801-1234
  • 2280
  • 22801-123

IPv4 address

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

Validates a properly formatted IPv4 address.

  • 192.168.1.1
  • 255.255.255.0
  • 256.0.0.1
  • 192.168.1

UUID / GUID

/i
^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$copy

Matches a valid UUID v1–v5.

  • 550e8400-e29b-41d4-a716-446655440000
  • not-a-uuid

Strong password

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

8+ chars requiring upper, lower, digit, and special char.

  • Secret@123
  • weakpassword
  • NoSpecial1

Numbers

Integer

^-?\d+$copy

Positive or negative whole number.

  • 42
  • -100
  • 3.14
  • 1e10

Decimal number

^-?\d+(\.\d+)?$copy

Integer or decimal, positive or negative.

  • 3.14
  • -0.5
  • .5
  • 1.

Hex color

^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$copy

3- or 6-digit hex color codes.

  • #ff5733
  • #FFF
  • ff5733
  • #gg0000

US currency

^\$?\d{1,3}(,\d{3})*(\.\d{2})?$copy

USD with optional $ and comma separators.

  • $1,234.56
  • 999.99
  • $1234.5

Dates & Times

ISO 8601 date

^\d{4}-\d{2}-\d{2}$copy

YYYY-MM-DD format.

  • 2026-06-04
  • 06/04/2026
  • 2026-6-4

US date (MM/DD/YYYY)

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

M/D/YY or MM/DD/YYYY with / or - separators.

  • 06/04/2026
  • 6-4-26
  • 2026-06-04

24-hour time

^([01]\d|2[0-3]):[0-5]\d(:[0-5]\d)?$copy

HH:MM and HH:MM:SS in 24-hour format.

  • 14:30
  • 09:05:00
  • 25:00
  • 9:5

12-hour time

/i
^(0?[1-9]|1[0-2]):[0-5]\d(:[0-5]\d)?\s?(AM|PM)$copy

H:MM AM/PM format.

  • 2:30 PM
  • 11:59:59 am
  • 14:00 PM

Text & Strings

URL slug

^[a-z0-9]+(?:-[a-z0-9]+)*$copy

Lowercase slug like blog-post-title.

  • my-blog-post
  • hello-world-123
  • Not A Slug
  • -leading-dash

Hashtag

/g
#[a-zA-Z]\w*copy

Finds #hashtags in text.

  • #hello #world
  • ## not-a-tag

@mention

/g
@[a-zA-Z0-9_]+copy

Finds @username mentions.

  • Hey @alice and @bob_99!
  • no mentions here

camelCase identifier

^[a-z][a-zA-Z0-9]*$copy

Starts lowercase, no spaces or special chars.

  • myVariableName
  • MyClass
  • has space

Duplicate consecutive words

/gi
\b(\w+)\s+\1\bcopy

Finds repeated adjacent words like "the the".

  • the the quick fox
  • no duplicates here

Blank / whitespace-only line

/m
^\s*$copy

Matches empty lines or lines with only spaces.

  • text

HTML & Code

HTML tag (to strip)

/g
<[^>]+>copy

Matches any HTML tag — use .replace() to strip markup.

  • <p>Hello <b>world</b></p>
  • plain text

HTML comment

/g
<!--[\s\S]*?-->copy

Matches <!-- ... --> HTML comments.

  • <!-- a comment -->
  • // not html

JS // comment

/g
\/\/[^\n]*copy

Matches single-line // comments.

  • const x = 1; // init
  • /* block comment */

CSS hex color (inline)

/g
#[0-9A-Fa-f]{3,6}copy

Finds hex colors within a larger string.

  • color: #ff5733; border: 1px #FFF
  • no color here