VOOZH about

URL: https://dev.to/zhihu_wu_dea1d82af01a04d7/6-regex-patterns-every-developer-should-have-bookmarked-bc0

⇱ 6 Regex Patterns Every Developer Should Have Bookmarked - DEV Community


I've lost count of how many times I've Googled "regex for email validation" at 11pm. So I finally wrote them down. Here are six patterns I use almost weekly — tested, commented, and ready to copy.

1. Extract URLs from text

/https?:\/\/[^\s"'<>]+/gi

Use this when scraping, cleaning user input, or pulling links out of a Slack dump. The [^\s"'<>]+ stops at whitespace or HTML delimiters instead of eating the rest of the line.

2. Match a valid IPv4 address

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

Ugly, yes. But it actually validates the range (0-255) instead of just matching \d+\.\d+\.\d+\.\d+ which would accept 999.999.999.999. Parse config files, extract IPs from logs — this one pays rent.

3. Strip HTML tags

/<[^>]*>/g

One-liner that removes everything between angle brackets. Not a full HTML parser (don't use it on nested malformed markup), but perfect for extracting plain text from simple HTML snippets or email bodies.

4. Match a CSS hex color

/#([a-fA-F0-9]{6}|[a-fA-F0-9]{3})\b/g

Catches both #FF5733 and #F53. I use this when auditing design tokens or writing a color-picker tool — it pulls every hex value out of a stylesheet in one pass.

5. Split a camelCase string into words

/([a-z0-9])([A-Z])/g  replace with $1 $2

myVariableName becomes my Variable Name. Useful for generating human-readable labels from code identifiers — I use it all the time when building admin panels from API field names.

6. Validate a reasonable email address

/^[^\s@]+@[^\s@]+\.[^\s@]{2,}$/i

No, it doesn't implement RFC 5322. But it catches typos, missing @ signs, and empty domains — which is 99% of what you need on a signup form. For actual email verification, send a confirmation link.


I test all my patterns in this free Regex Tester before committing them — it highlights matches, shows capture groups, and has common presets. Saves me from alt-tabbing to a Node REPL every five minutes.

What's the one regex you keep copy-pasting? Drop it in the comments.