Menu

1 / 2

6 Regex Patterns Every Developer Should Have Bookmarked

DEV Community·zhihu wu·6 months ago
#j0KbQTRZ
#regex#webdev#javascript#programming#enter
Reading 0:00
15s threshold

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

Enter fullscreen mode

Exit fullscreen mode

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 /

Enter fullscreen mode

Exit fullscreen mode

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.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.