Who likes writing regex? No one. Who needs regex? Everyone!
So stop googling regex. Start pasting them. π§
Regex Cookbook is a practical, language-agnostic, copy-paste-ready cheat sheet of real-world RegExp patterns, organized by category. From emails and dates to URLs, binary formats, colors, and beyond β no theory, just patterns that work.
- π§± Basics
- π Date
- π Time
- β DateTime
- π§ Email
- π Password
- π± Phone Numbers
- π URLs
- π File Extensions
- 𧬠Syntax
- π» HTML
- π¨ Colors
- π¦ Data Structures
- π IPs
- π§ Text
- π’ Numbers
- π° Currency & Symbols
- π³ Finance
- π Separated Values
- π Slugs & Identifiers
- π§Ή Shortcodes
- π Repetition & Structure
- π₯οΈ Binary & Low-Level
- π Cryptography
- π«π· France-Specific
- π€ Contributing
- π License
/\d+/
Matches one or more digits.
- β 123
- β 007
- β abc
/[a-z]+/
Matches one or more lowercase English letters.
- β hello
- β HELLO
- β Hello123
/[A-Z]+/
Matches one or more uppercase English letters.
- β ABC
- β HELLO
- β Hello
- β abc
/[.,;:!?'"(){}\[\]-]+/
Matches one or more punctuation characters.
- β ,!?
- β (hello)
- β hello
/[\p{L}]/u
Matches any letter from any language (including accents and scripts like Arabic, Cyrillic, etc.).
- β Γ©
- β Γ§
- β Γ¦
- β Ψ£
- β 123
Requires the Unicode flag /u
.
/[\p{Emoji}]/u
Matches any emoji character.
- β π
- β π‘
- β πββοΈ
- β Hello
Requires modern engines (JS ES2018+). Not all engines support \p{Emoji}
.
/\b(hello|hi|hey)\b/i
Matches exactly βhelloβ, βhiβ, or βheyβ as whole words, case-insensitive.
- β hello
- β Hi
- β HEY
- β helloo
- β ohhey
Add or remove words inside (word1|word2|...)
to adjust.
/^\d{4}-\d{2}-\d{2}$/
Matches a date formatted as year-month-day (ISO 8601).
- β 2023-12-25
- β 1999-01-01
- β 12-25-2023
- β 2023/12/25
Change -
to /
or .
if your date format uses different separators.
/^(0[1-9]|1[0-2])\/(0[1-9]|[12]\d|3[01])\/\d{4}$/
Matches dates like month/day/year.
- β 12/25/2023
- β 01/01/2000
- β 25/12/2023
- β 1/1/2000
This forces 2-digit month and day. Use \/?
if the slash is optional.
/^([01]\d|2[0-3]):[0-5]\d$/
Matches a valid time in 24-hour format.
- β 09:30
- β 23:59
- β 24:00
- β 9:5
To allow optional leading zeros, change [01]\d
to \d{1,2}
.
/^(0?[1-9]|1[0-2]):[0-5]\d\s?(AM|PM)$/i
Matches 12-hour time with AM or PM suffix.
- β 10:45 AM
- β 1:30 pm
- β 13:00 PM
- β 10:75 AM
The i
at the end makes it case-insensitive.
/^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[\+\-]\d{2}:\d{2})$/
Full ISO datetime with timezone.
- β 2023-10-12T14:48:00Z
- β 2023-10-12T14:48:00+02:00
- β 2023-10-12 14:48:00
/^\d{10}$/
Matches a 10-digit timestamp in seconds.
- β 1617181723
- β 161718 (too short)
- β 2023-01-01
/^[\w.-]+@[\w.-]+\.[a-zA-Z]{2,}$/
Matches standard email addresses.
- β user@example.com
- β john.doe@mail.co.uk
- β user@@example.com
- β user@.com
To restrict domains (e.g. only .com), replace \.[a-zA-Z]{2,}
with \.com
.
/^(?=.*[0-9])(?=.*[A-Z])(?=.*[a-z])(?=.*\W*)(?!.*\s).{8,16}$/
Accepts at least one number, one uppercase letter, one lowercase letter, and zero or more special characters, with a length between 8 to 16 characters.
- β Password1?
- β *pwD123!:?+
- β password
- β my super secure password
Change the numbers in {8,16}
to adjust length. Use {8,}
for "8 or more characters".
/^\+\d{1,3}[\s.-]?\(?\d+\)?[\s.-]?\d+[\s.-]?\d+$/
Matches common international formats.
- β +33 6 12 34 56 78
- β +1 (123) 456-7890
- β 06.12.34.56.78 (without +)
/^\+(\d{1,4})/
Extracts the international prefix from a phone number starting with +
.
- β
+33 6 12 34 56 78 β captures
33
- β
+1 (123) 456-7890 β captures
1
- β 0033 6 12 34 56 78
/^(?:\+|00)\d{1,4}[\s.-]?\d+([\s.-]?\d+)*$/
Matches international numbers that start with either +
or 00
, followed by digits.
- β +33 6 12 34 56 78
- β 0033 6 12 34 56 78
- β +1-123-456-7890
- β 06 12 34 56 78
Use (?:\+|00)
to support both styles for international dialing.
/https?:\/\/([\w.-]+)\.[a-z]{2,}(\/[\w\-._~:\/?#[\]@!$&'()*+,;=]*)?/i
Matches URLs with http or https, optional subdomain and path.
- β https://example.com
- β http://blog.example.co.uk/path/to/page
- β ftp://example.com
To allow ports (e.g. :3000
), add (:\d+)?
after the domain.
/^[\w-]+\.[a-z]{2,}$/
Matches only the domain.
- β google.com
- β example.co.uk
- β http://google.com
- β google
/^https:\/\/[a-z0-9-]+\.[a-z]{2,}$/
Matches only https full domains without subdomains.
/^ftp:\/\/[\w.-]+\.[a-z]{2,}(\/.*)?$/
Matches FTP links with optional path.
- β ftp://files.example.com
- β ftp://example.com/folder/file.txt
- β http://example.com
/^https?:\/\/(\d{1,3}\.){3}\d{1,3}(\/.*)?$/
Matches an IP-based URL.
- β http://192.168.0.1
- β https://127.0.0.1/index.html
- β 192.168.0.1
/^https?:\/\/localhost:\d{2,5}(\/.*)?$/
Matches localhost with port and optional path.
- β http://localhost:3000
- β https://localhost:8080/admin
- β localhost:3000
/^[\w,\s-]+\.[A-Za-z]{2,4}$/
Basic filename with extension.
- β file.txt
- β my photo.jpeg
- β file
/^.+\.(jpg|jpeg|png|gif|bmp|svg)$/i
Common image formats.
- β logo.png
- β photo.JPG
- β file.pdf
/^.+\.pdf$/i
- β document.pdf
- β document.docx
/^[a-z]+$/
All lowercase, no separator.
- β hello
- β Hello
- β helloWorld
/^[A-Z]+$/
All uppercase, no separator.
- β HELLO
- β Hello
- β HELLO_WORLD
/^[a-z]+(?:[A-Z][a-z]*)+$/
Starts lowercase, words joined with uppercase letters.
- β helloWorld
- β HelloWorld
- β hello_world
/^(?:[A-Z][a-z]+)+$/
Each word starts with a capital letter.
- β HelloWorld
- β helloWorld
- β Hello_World
/^[a-z]+(_[a-z]+)+$/
Lowercase words separated by underscores.
- β hello_world
- β foo_bar_baz
- β Hello_World
- β helloWorld
/^[A-Z]+(_[A-Z]+)+$/
All uppercase with underscores.
- β HELLO_WORLD
- β Hello_World
- β hello_world
/^([A-Z][a-z]+_)+[A-Z][a-z]+$/
PascalCase segments separated by underscores.
- β Hello_World_Example
- β hello_world
- β HelloWorld
/^[a-z]+(-[a-z]+)+$/
Lowercase words separated by hyphens.
- β hello-world
- β Hello-World
- β helloWorld
/^[A-Z]+(-[A-Z]+)+$/
Uppercase words separated by hyphens.
- β HELLO-WORLD
- β hello-world
/^([A-Z][a-z]+-)+[A-Z][a-z]+$/
PascalCase segments separated by hyphens.
- β Hello-World-Example
- β HelloWorld
- β hello-world
/>([^<]+)</
Captures the content between tags.
- β
<b>Hello</b>
βHello
- β
<b></b>
/<(\w+)([^>]*)>(.*?)<\/\1>/
Captures entire tag with content.
- β
<div class="x">Hello</div>
- β
<img src="x.jpg"/>
/<\w+[^>]*\/>/
Matches self-closing HTML tags.
- β
<img src="image.jpg" />
- β
<br/>
- β
<div>Hello</div>
/<!--[\s\S]*?-->/
Matches HTML comments.
- β
<!-- This is a comment -->
/<(\w+)[^>]*>([^<]*)<\/\1>/
Simple tag pair with content, not nested.
- β
<b>Hello</b>
- β
<b><i>Bold</i></b>
/^#(?:[0-9a-fA-F]{3}){1,2}$/
Matches 3- or 6-digit hexadecimal color codes.
- β #fff
- β #FFFFFF
- β #123abc
- β #abcd
- β 123456
/^rgb\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*\)$/
Matches an rgb()
color with values 0β255.
- β rgb(255, 0, 127)
- β rgb( 0 , 255 , 64 )
- β rgb(300,0,0)
- β rgba(255,0,0,0.5)
/^rgba\(\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(\d{1,3})\s*,\s*(0|1|0?\.\d+)\s*\)$/
Matches an rgba()
color with alpha 0β1.
- β rgba(255, 0, 127, 0.5)
- β rgba(0, 0, 0, 1)
- β rgba(0, 0, 0)
- β rgba(255,255,255,1.5)
/^hsl\(\s*(\d{1,3})\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)$/
Matches hsl()
values where hue is 0β360, sat/light are percentages.
- β hsl(120, 100%, 50%)
- β hsl(0,0%,0%)
- β hsl(400,100,50)
- β hsl(120, 50, 50)
/^oklch\(\s*([\d.]+)\s+([\d.]+)\s+([\d.]+)(?:\s*\/\s*([\d.]+))?\s*\)$/
Matches OKLCH format with optional alpha (/0.5
).
- β oklch(0.628 0.12 281.07)
- β oklch(0.628 0.12 281.07 / 0.5)
- β oklch(0.628, 0.12, 281.07)
- β oklch(0.6 0.2)
/^cmyk\(\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*,\s*(\d{1,3})%\s*\)$/
Matches CMYK color values with percentages for cyan, magenta, yellow, and black.
- β cmyk(0%, 100%, 100%, 0%)
- β cmyk(25%, 10%, 0%, 80%)
- β cmyk(100, 0, 0, 0) (missing
%
) - β cmyk(0%, 0%, 0%)
/^Pantone\s?(\d{3,4})(\s?[A-Z]{1,2})?$/
Matches Pantone codes with optional suffix like C
, U
, etc.
- β Pantone 300
- β Pantone 300 C
- β Pantone 485U
- β Pantone red
- β Pantone 30A
/^RAL\s?(\d{4})$/
Matches classic RAL color codes.
- β RAL 3020
- β RAL3003
- β RAL red
- β RAL 123
π‘ You can extend this to RAL Design (e.g. RAL 210 50 20
) with:
/^RAL\s(\d{3})\s(\d{2})\s(\d{2})$/
- β RAL 210 50 20
- β RAL 2105020
- β RAL 210-50-20
/^\{\s*"[^"]+"\s*:\s*.+\}$/
Matches a flat JSON object with a single key-value pair.
- β {"name": "Jean"}
- β {"name": "Jean", "age": 30} (multi-pair, nested)
- β name: "Jean"
Use a proper parser for nested or valid JSON.
/<(\w+)[^>]*>(.*?)<\/\1>/
Matches any XML element with opening and closing tags.
- β
<data>Hello</data>
- β
<tag attr="x">Value</tag>
- β
<data />
/^\s*[\w.-]+\s*:\s*.+$/
Matches a basic YAML key and value on a single line.
- β name: Jean
- β user_id: 12345
- β - item: value (list format)
- β name "Jean"
/^(\d{1,3}\.){3}\d{1,3}$/
Matches standard IPv4 format.
- β 192.168.0.1
- β 8.8.8.8
- β 999.999.999.999 (invalid range, but matches)
For stricter check, validate each octet is <= 255 programmatically.
/^(\d{1,3}\.){3}\d{1,3}:\d+$/
Matches an IPv4 address followed by a port (e.g. :8080
).
- β 127.0.0.1:3000
- β 192.168.1.10:80
- β 192.168.1.10
- β localhost:3000
/^(\d{1,3}\.){3}\d{1,3}\/\d{1,2}$/
Matches IPv4 blocks in CIDR notation.
- β 192.168.0.0/24
- β 10.0.0.0/8
- β 192.168.0.1
- β /24
/^([a-fA-F0-9]{1,4}:){7}[a-fA-F0-9]{1,4}$/
Matches full-form IPv6 addresses.
- β
2001:0db8:85a3:0000:0000:8a2e:0370:7334
- β
::1
(shortened form not supported by this regex)
/^[\x00-\x7F]+$/
Accepts only basic ASCII characters.
- β Bank Statement 10
- β DΓ©claration bancaire
- β StatementπΈ
/^[A-Za-z\s.,;:!?'"()-]+$/
Only allows ASCII letters and punctuation.
- β Bank Statement
- β Statement 10
- β RΓ©sumΓ©
/^(?!.*[\u{1F600}-\u{1F64F}]).*$/u
Rejects any string containing emojis in the emoticon range.
- β Hello world
- β Hello π
Use different unicode ranges for other emoji types.
/^\d+$/
Only numbers.
- β 123456
- β 123 456
- β 12a34
/^\d+(\.\d+)?$/
Matches numbers with optional decimal.
- β 12
- β 12.34
- β 12,34
To use comma instead of dot, replace \.
with ,
.
/^-?\d+$/
Matches whole numbers, including negative ones.
- β 42
- β -15
- β 3.14
- β 1,000
/^-?\d+(\.\d+)?$/
Matches decimal numbers with optional minus sign.
- β -12.34
- β 5.0
- β 42
- β 5,0
/^\d+(?:\.\d{2})?$/
Matches prices with optional 2 decimal places.
- β 10
- β 10.00
- β 10.0
- β 10.000
Change \.
to ,
for locales using comma separators.
/^\(\d{1,3}(?:[.,]?\d{3})*(?:[.,]\d+)?\)$/
Matches negative numbers written in parentheses, like (1,234.56)
or (1234,56)
. Accepts both .
and ,
for decimal or thousand separators.
- β (123)
- β (1,234.56)
- β (1234,56)
- β (1.234,99)
- β -123
- β ( 123 )
- β (1234.567.89)
π‘ To force dot or comma for decimal, replace [.,]
by \.
or ,
.
π‘ If you donβt use thousands separators, simplify to:
/^\(\d+(?:[.,]\d+)?\)$/
/[\$\β¬\Β£\Β₯\β½\βΉ]/
Matches standalone currency symbols.
- β $
- β β¬
- β Β£
- β Β₯
- β 100
- β 10β¬
Add or remove symbols inside the brackets to customize.
/[\$\β¬\Β£\Β₯\β½\βΉ]\s?\d+(?:[.,]\d{1,2})?/
Matches currencies like $100
, Β£ 100.83
, with or without space between symbol and number. Accepts comma or dot as decimal.
- β $100
- β β¬ 100,50
- β Β£99.99
- β 100β¬
Change [.,]
to force one decimal separator.
Use [.,]\d{2}
to force 2 decimals.
/\d+(?:[.,]\d{1,2})?\s?[\$\β¬\Β£\Β₯\β½\βΉ]/
Matches formats like 100β¬
, 1.500,00 β¬
, with optional space.
- β 10β¬
- β 99.99β¬
- β 1 000,00 β¬
- β $100
/\d+(?:[.,]\d+)?\s?%/
Matches percentages with or without decimal and space.
- β 20%
- β 20 %
- β 20.5%
- β 20,5 %
- β %20
/^[A-Z]{2}\d{2}[A-Z0-9]{11,30}$/
Matches IBAN format (country code, checksum, BBAN).
- β FR7630006000011234567890189
- β DE89370400440532013000
- β FR76 3000 6000 0112... (spaces)
Remove spaces before testing. Specific formats vary by country.
/^[A-Z]{6}[A-Z0-9]{2}([A-Z0-9]{3})?$/
Matches standard 8 or 11-character SWIFT/BIC codes.
- β BNPAFRPP
- β DEUTDEFF500
- β BICFRPP123456
/^\d{4}[- ]?\d{4}[- ]?\d{4}[- ]?\d{4}$/
Matches 16-digit credit card number with optional spaces or dashes.
- β 4111 1111 1111 1111
- β 4111-1111-1111-1111
- β 4111111111111111
- β 4111-1111-1111
This does not validate via the Luhn algorithm.
/^([^,\n]+,)*[^,\n]+$/
Matches comma-separated values on one line.
- β a,b,c
- β a,,c
- β a,b,\nc
/^([^\t\n]+\t)*[^\t\n]+$/
Matches tab-separated values on one line.
- β a\tb\tc
- β a\t\tc
/^([^;\n]+;)*[^;\n]+$/
Matches semicolon-separated values.
- β a;b;c
- β a;;c
- β a;b;\nc
/^[a-z0-9]+(?:-[a-z0-9]+)*$/
Lowercase, hyphenated, SEO-friendly string.
- β hello-world
- β my-article-123
- β Hello_World
- β hello--world
/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i
Matches a version 4 UUID.
- β 550e8400-e29b-41d4-a716-446655440000
- β 550e8400e29b41d4a716446655440000
/<([^<>]+)>/
Matches anything between <
and >
, excluding nested or malformed tags.
- β
<shortcode>
- β
<user_name>
- β <>
- β <shortcode
/\(([^()]+)\)/
/\[([^\[\]]+)\]/
/\{([^{}]+)\}/
/\{\{([^{}]+)\}\}/
/<\-([^<>]+)\->/
[^...]
ensures the content does not include the delimiters themselves.- Use
g
(global flag) if you want to match multiple shortcodes in one string. - Escape characters like
(
,[
,{
using\
inside the regex.
/(.)\1{2,}/
Matches 3 or more of the same character in a row.
- β aaa
- β 1111
- β ababab
- β aa
Change {2,}
to {N-1,}
to match N repetitions.
Example: {6,}
matches 7 or more.
/([01]{8})/g
Matches binary data in groups of 8 bits (octets).
- β
0101010101010101 β
01010101
,01010101
- β 1111000010101010
- β 0101010 (only 7 bits)
Use with the global flag /g
to match multiple octets in a stream.
/^([0-9A-Fa-f]{2}[:-]){5}([0-9A-Fa-f]{2})$/
Matches standard MAC addresses with :
or -
as separator.
- β 00:1A:2B:3C:4D:5E
- β 00-1A-2B-3C-4D-5E
- β 001A2B3C4D5E
- β 00:1A:2B:3C:4D
/\b(?:0x)?[0-9a-fA-F]+\b/
Matches raw hex numbers, optionally starting with 0x
, but no #
.
- β 0x1A3F
- β a4f3
- β DEADBEAF
- β #FF00FF
/^\d+(\.\d+)?\s?(B|KB|MB|GB|TB)$/i
Matches common file size formats.
- β 100KB
- β 5.6 MB
- β 2048B
- β 5,6MB (comma instead of dot)
/^[A-Z]:\\(?:[^\\\/:*?"<>|\r\n]+\\)*[^\\\/:*?"<>|\r\n]*$/i
Matches a full Windows-style path.
- β C:\Users\MyFolder\File.txt
- β D:\Backup\2024\Logs\log.txt
- β /usr/local/bin
/^\/(?:[^\/\n]+\/)*[^\/\n]+$/
Matches a Unix-like full path.
- β /usr/local/bin/python
- β /var/log/nginx/access.log
- β C:\Program Files
/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/
Matches a valid Base64-encoded block.
- β TWFu
- β U29tZSBkYXRhIHdpdGggZXF1YWwgcGFkZGluZw==
- β This is not base64
/^[a-f0-9]{32}$/i
- β d41d8cd98f00b204e9800998ecf8427e
- β too_short_hash
/^[a-f0-9]{40}$/i
- β da39a3ee5e6b4b0d3255bfef95601890afd80709
- β 123abc
/^[a-f0-9]{64}$/i
- β e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
- β short_hash
/^[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+\.[A-Za-z0-9-_]+$/
Matches the 3-part JWT format (header.payload.signature), base64-url encoded.
- β eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0β¦
- β abc.def
- β ey... (missing part)
This doesn't validate the signature or payload content.
/^\d{9}$/
- β 732829320
- β 732 829 320
- β 1234567890
/^\d{14}$/
- β 73282932000074
- β 732 829 320 00074
/^FR[\dA-Z]{2}\d{9}$/
- β FR40303265045
- β FRXX732829320
- β FR40303265045123
- β FR40 3032 6504 5
Check rules per country for prefix and control key logic.
You're welcome to contribute new patterns to this cheat sheet, or improve existing, either by the use of PR or issues!
To keep things accessible and consistent, please follow these guidelines:
- Use the existing format for each entry:
- A clear title (
###
level) - A brief, plain-language explanation of the use case
- The regular expression inside a code block
- β Working examples and β non-matching examples
- A short tip to adapt or tweak the expression (when applicable)
- A clear title (
Example:
### Match UPPERCASE words
/^[A-Z]+$/
Matches strings with only uppercase letters.
- β
HELLO
- β
REGEXP
- β Hello
- β hello123
To allow numbers too, change to `/^[A-Z0-9]+$/`
---
- Keep it simple and free of regex jargon β this is meant to be readable by everyone
- Make sure every pattern is tested and working before submitting
- Pull requests are reviewed and tested before merging β no untested expressions please π
This project is licensed under the MIT License. Feel free to use, copy, modify, and distribute β contributions welcome!