Skip to content

Instantly share code, notes, and snippets.

@peta
Last active October 24, 2018 15:18
Show Gist options
  • 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:[email protected]:81/directory/file.ext?query=1#anchor');
// Variable "results" now contains the fragments in the following order
var keysInOrderTheyAppear = [
"href", // http://user:[email protected]:81/directory/file.ext?query=1#anchor
"origin", // http://user:[email protected]: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