Back to the tool

URL Validation Regex: Why Most Patterns Are Wrong

What is the best regex to validate a URL? The honest answer is that there is no good one, and the famous ones are worse than useless. The most copied URL pattern on the internet runs to thousands of characters, still misses valid URLs, and can freeze your server on a malicious input. These days I let the parser decide and keep regex to the cheap pre-check.

The problem starts with what a URL actually is. Schemes, optional userinfo, hosts that can be domain names, IPv4, or bracketed IPv6, optional ports, paths, query strings full of encoded characters, fragments, internationalized domain names. A regex that accepts all of that and rejects everything else is not a pattern, it is a parser wearing a costume. And parsers written as regex have a failure mode that plain parsers do not.

The two ways URL patterns fail

The first failure is rejection: the pattern says no to a URL that is perfectly fine. The classic copied-from-the-internet pattern demands http or https, so it rejects ftp links, custom app schemes, and protocol-relative URLs. It bakes in assumptions about TLD length from a decade ago, so newer long TLDs fail. It chokes on localhost with a port, on IPv6 literals in brackets, on userinfo like user:pass@host, on unicode domains. Every one of those is a real URL someone will paste into your form, and every rejection is a support ticket.

The second failure is worse: catastrophic backtracking. Patterns with nested quantifiers, things like (a+)+ or overlapping optional groups, can take exponential time on certain inputs. Feed one a long, slightly malformed URL and the regex engine spins, burning a CPU core while your server stops answering. This has a name, ReDoS, regular expression denial of service, and URL validators are one of its favorite victims because URLs are long and attacker-controlled. A validator that can be turned into a freeze button is not a validator.

What I use instead

Every major language ships a real URL parser. Use it. In JavaScript, new URL() follows the WHATWG URL spec and either returns a parsed object or throws. In Python, urllib.parse does the same job. The parser handles the grammar correctly because implementing the grammar is its entire job, and it cannot be ReDoSed by a hostile string the way a hand-rolled pattern can.

The pattern I keep around is deliberately dumb. It checks that the string starts with http:// or https://, contains no whitespace, and has something that looks like a host. Something like:

^https?://[^\s/$.?#].[^\s]*$

That pattern is not validating the URL. It is doing a cheap sanity check before handing the string to the parser, which does the real work. Regex handles the shape check; the parser handles the grammar.

One more honest limit, from the security side. Even a correctly parsed URL is not necessarily a safe URL. A domain can resolve to an internal address after your check passes, a classic DNS rebinding trick, and a legitimate-looking URL can redirect to somewhere hostile. Input validation catches the obvious cases. If the URL will be fetched server-side, you need validation at request time too: resolve the host yourself, check the IP, and validate redirect targets. The regex was never going to do that job.

Frequently asked questions

What is the best regex to validate a URL?

There is not a good one. URL grammar is too complex for a pattern to handle correctly, and complex patterns risk catastrophic backtracking. Use your language's built-in URL parser, like new URL() in JavaScript or urllib.parse in Python, with a simple regex only as a cheap pre-check.

Why do URL regexes cause ReDoS?

Nested and overlapping quantifiers, such as (a+)+, can make the regex engine try an exponential number of paths on certain inputs. A long, malformed URL triggers the worst case, freezing the thread. Keeping patterns simple and delegating to a real parser avoids the problem.

Should I validate URLs with regex at all?

Only for a rough shape check: correct scheme, no whitespace, something host-like. Let a real parser decide whether the URL is well formed. For anything security-sensitive, validate again at request time, after DNS resolution, not just at input.

How do I handle localhost URLs in validation?

A parser accepts them fine. Most hand-written regexes do not, because they assume a dotted domain with a TLD. If your app needs localhost URLs, for development webhooks or callbacks, test them explicitly. A trailing dot on localhost and bracketed IPv6 like [::1] are the edge cases that break naive patterns.

How should I test a URL validation pattern?

Include adversarial cases: the shortest valid URL, the longest, encoded characters in the query string, ports, fragments, IPv6 literals, unicode domains, and strings that should fail. Confirming a rejection matters as much as confirming a match. Test your pattern against real inputs here before it ships.

Related reading: Email Validation Regex: The Mistakes Everyone Makes · Catastrophic Backtracking: The Regex Bug That Freezes Servers · Greedy vs Lazy Matching: The Regex Distinction That Changes Everything · Phone Number Regex vs libphonenumber: Stop Writing Your Own

Try the tool

Customize a regex template and get a plain-English explanation of every group, named or not. Build a regex template and stop counting parentheses.

Get new free tools by email

Want the next guide in your inbox? I publish one practical guide per new tool. Subscribe to the free newsletter on Substack. No spam, unsubscribe anytime.