Explain Codes LogoExplain Codes Logo

What is a good regular expression to match a URL?

javascript
regex
url-validation
javascript-regex
Alex KataevbyAlex Kataev·Dec 18, 2024
TLDR

Here's your go-to regex for URLs:

/https?:\/\/\S+\.\S+/g

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:

/(https?:\/\/)?(www\.)?\S+\.\S+/g

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:

/https?:\/\/(?:www\.)?\S+\.\S+(?:\/[^\s]*)?/gi

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:

/(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+/gi

Welcomes ftp protocols and charmed by a wide array of Unicode character sets.

Filtering unwanted protocols

Set up a bouncer with negative lookahead:

/https?:\/\/(?!\S*:\/\/)\S+\.\S+/g

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:

/((?!-)[A-Za-z0-9-]{1,63}(?<!-)\.)+[A-Za-z]{2,6}/gi

No starting or ending with hyphens. The first and final impression matters.

Real-time validation: letting your users know

document.getElementById('urlInput').addEventListener('keyup', function(e) { const whoLetTheDogsOut = urlRegex.test(e.target.value); // The feedback. It's alive! });

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