Skip to main content
hank-builds.com

URL regex

Matches HTTP and HTTPS URLs.

/https?://[\w.-]+(?:\.[a-z]{2,})(?:/[\w./?%&=-]*)?/gi

What it matches

An http or https scheme, a host of letters, digits, dots and hyphens ending in a dotted suffix of two or more letters, then an optional path that may carry a query string. The path class allows slashes, dots, percent escapes, ampersands and equals signs, so most tracking URLs survive intact.

Railroad diagram

Read left to right. Every path through the diagram is a string the pattern accepts.

"h" "t" "t" "p" "s" ":" \/ \/ [\w.-] \. {2,} [a-z] \/ [\w.\/?%&=-]

Drag to pan. Ctrl+scroll to zoom.

Breakdown

gFind all matches instead of stopping after the first
iLetters match both upper and lower case
hLiteral "h"
tLiteral "t"
tLiteral "t"
pLiteral "p"
s?Literal "s", optional
:Literal ":"
\/Literal "/"
\/Literal "/"
[\w.-]+Match one of: Word character (a-z, A-Z, 0-9, _), Literal ".", Literal "-", one or more
\.Literal "."
[a-z]{2,}Match one of: Literal "a"–Literal "z", 2 or more times
\/Literal "/"
[\w.\/?%&=-]*Match one of: Word character (a-z, A-Z, 0-9, _), Literal ".", Literal "/", Literal "?", Literal "%", Literal "&", Literal "=", Literal "-", zero or more

Test cases

Matches

  • https://example.com
  • http://test.org/path?q=1
  • https://sub.domain.co.uk/a/b

Does not match

  • example.com
  • ftp://files.example.com
  • https://localhost

Caveats

Only http and https: ftp, mailto and protocol-relative //example.com are all rejected, as is a bare host with no scheme. It requires a dotted suffix, so http://localhost:3000 does not match. Ports and fragments are not modelled, and a trailing #anchor stops the match early.

More common patterns

Test this against your own input in the regex tester, browse the full pattern library, or check the syntax cheat sheet.