Skip to main content

Developer

Regex Cheatsheet for Web Developers

Regex is universal across most languages but the pitfalls are too. Here's the practical cheatsheet for the patterns that come up daily in web development.

Email (RFC-5322 lite)

/^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z] 2 $/ — covers 99% of real emails. The full RFC regex is 6KB long and accepts strings nobody actually uses.

URL

/^https?:\/\/[a-zA-Z0-9.-]+(\.[a-zA-Z] 2 )+(\/.*)?$/ — http/https URLs with proper TLDs. Better: use new URL(s) and catch errors.

IPv4

/^(\d 1 3 \.) 3 \d 1 3 $/ — basic. Strict (no >255): /^((25[0-5]|2[0-4]\d|[01]?\d\d?)\.) 3 (25[0-5]|2[0-4]\d|[01]?\d\d?)$/.

Common pitfalls

. matches anything except newline by default — use /s flag if needed. * is greedy.*? is lazy. Always anchor with ^ and $ unless you want substring matches.

Want to verify your setup?

Run the check now