Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Javascript URL parsing regex
const regex = /^((?<scheme>[^:\/?#]+):)?(\/\/(?<authority>(?<domain>[^\/?#:]*)(:(?<port>[0-9]*))?))?((?<path>[^?#]*)\\?)?((?<query>([^#]*)))?(#(?<fragment>(.*)))?/;
'foo://example.com:8042/over/there?name=ferret#nose'.match(regex);
/* Result:
authority: "example.com:8042"
domain: "example.com"
fragment: "nose"
path: "/over/there"
port: "8042"
query: "?name=ferret"
scheme: "foo"
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.