RegexCalc Regex Engine

URL Regex Tester

A URL pattern for http and https links, pre-loaded in the tester. Below it, a table shows what it accepts and rejects, including the cases that trip most URL regexes: localhost, letter case and non-ASCII hosts.

Regular Expression JAVASCRIPT REGEX
/ / gm
Match Status
2 matches found
Pattern Valid
Recipes:
Test String
Extracted Matches

What this URL pattern accepts and rejects

The pattern accepts http and https URLs whose host has a dot and a short final label, then an optional path, query and fragment. It checks shape, not that a host exists, and it has opinions worth knowing about localhost, letter case and non-ASCII hosts.

Each input below was tested on its own against the pattern above when this page was built, with no flags, so ^ and $ anchor the whole value. Paste any of them into the tester to see the same result.

InputResultWhy it matters
https://github.com/walshie4MatchesAn ordinary link.
https://example.com:8443/path?q=1#fragMatchesA port, a query string and a fragment all pass.
https://192.168.0.1/xMatchesAn IP address passes, because its last octet satisfies the final-label rule.
https://a.bMatchesAccepted, though it is not a real domain: the pattern checks shape only.
http://localhost:8080/testNo matchRejected: the host needs a dot, so localhost fails with or without a port. Allow it separately in development tools.
HTTPS://EXAMPLE.COMNo matchRejected: the scheme is matched case-sensitively. Add the i flag if users paste upper-case URLs.
ftp://example.comNo matchRejected: only http and https are allowed.
https://example.com/path with spaceNo matchRejected: spaces must be percent-encoded as %20.
https://例子.测试No matchRejected: a Unicode host fails, although browsers accept it.
https://xn--fsqu00a.xn--0zwm56dMatchesThe same host in its ASCII (punycode) form passes, so convert with new URL() first if you need Unicode hosts.

More from RegexCalc

Guides

All tools →All 13 guides →

Frequently Asked Questions

Why does the URL regex reject http://localhost:8080?

Because the host must contain a dot followed by a short final label, and localhost has none. Ports are fine on an ordinary host: https://example.com:8443/path matches. If development URLs matter, allow localhost with a separate check.

Does this URL regex handle upper-case or Unicode URLs?

Not as written. The scheme is matched case-sensitively, so HTTPS://EXAMPLE.COM fails until you add the i flag, and Unicode hosts fail while their punycode form passes. Parsing with new URL() first fixes both, because it lower-cases the scheme and host and converts the host to punycode.

Developer Sponsor / Partner
Copied to clipboard!