Skip to content

Instantly share code, notes, and snippets.

@peta
Last active October 24, 2018 15:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save peta/58a222cd25526bcf5188a4d517ab87c5 to your computer and use it in GitHub Desktop.
Save peta/58a222cd25526bcf5188a4d517ab87c5 to your computer and use it in GitHub Desktop.
var urlParseRegex = /^(?:(?:(([^:\/#\?]+:)?(?:(?:\/\/)(?:(?:(?:([^:@\/#\?]+)(?:\:([^:@\/#\?]*))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((?:\/?(?:[^\/\?#]+\/+)*)(?:[^\?#]*)))?(\?[^#]+)?)(#.*)?/;
var results = urlParseRegex.exec('http://user:pass@host.com:81/directory/file.ext?query=1#anchor');
// Variable "results" now contains the fragments in the following order
var keysInOrderTheyAppear = [
"href", // http://user:pass@host.com:81/directory/file.ext?query=1#anchor
"origin", // http://user:pass@host.com:81
"protocol", // http:
"username", // user
"password", // pass
"host", // host.com:81
"hostname", // host.com
"port", // 81
"pathname", // /directory/file.ext
"search", // ?query=1
"hash" // #anchor
];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment