URL regex
Matches HTTP and HTTPS URLs.
/https?://[\w.-]+(?:\.[a-z]{2,})(?:/[\w./?%&=-]*)?/giWhat 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.
Drag to pan. Ctrl+scroll to zoom.
Breakdown
gFind all matches instead of stopping after the firstiLetters match both upper and lower casehLiteral "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 moreTest cases
Matches
https://example.comhttp://test.org/path?q=1https://sub.domain.co.uk/a/b
Does not match
example.comftp://files.example.comhttps://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.