What is a good regular expression to match a URL?
Here's your go-to regex for URLs:
This bad boy 🔥 latches onto HTTP/HTTPS links, sidesteps whitespace, and requires a domain and TLD. You're all set to go!
Regex anatomy: a quick walkthrough
Let's dissect the beast:
https?
: Matches http, trailing s is optional (supports HTTPS).:\/\/
: Double forward slashes after the colon. No mercy.\S+
: No whitespace needs to apply. Matches one or more non-whitespace characters.\.\S+
: Needs a dot followed by non-whitespace characters (handles TLDs).
Increasing inclusivity: respecting URL diversity
Upgrade the regex to respect variations in URLs:
Adds www prefix and tolerates missing http://. We believe in diversity. ✊
Accommodating paths and query strings
Some URLs come with extra luggage. Expand your regex's carrying capacity:
Now supports paths and query strings. Globe-trotters, assemble! 🌍
When a picture paints a thousand characters
Regex advanced toolkit: handle complex situations
Supporting international URLs & Unicode characters
Here's a multi-lingual regex for the cosmopolitans:
Welcomes ftp protocols and charmed by a wide array of Unicode character sets.
Filtering unwanted protocols
Set up a bouncer with negative lookahead:
Rejects URLs with excess protocols, like cutting a line at the club.
Just domain names, please
Needing a simpler regex for domain names? Here you go:
No starting or ending with hyphens. The first and final impression matters.
Real-time validation: letting your users know
Apply real-time URL validation. Because fast feedback feels good.
The real world isn't a textbook: testing your regex
Make regex101 your testing buddy, and validator.js your second pair of eyes to catch gremlins in your regex.
The brains behind the operation
Lessons, tips, and wisdom nuggets ftw
Was this article helpful?