Skip to content

Instantly share code, notes, and snippets.

@asika32764
Created December 6, 2018 16:37
Show Gist options
  • Select an option

  • Save asika32764/cf1803c08450dfcc80d192571ffd1e1f to your computer and use it in GitHub Desktop.

Select an option

Save asika32764/cf1803c08450dfcc80d192571ffd1e1f to your computer and use it in GitHub Desktop.
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