Skip to content

Instantly share code, notes, and snippets.

@AdamMadrzejewski
Forked from jlong/uri.js
Last active September 2, 2015 14:25
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 AdamMadrzejewski/24612ef0caf79e6c4fb6 to your computer and use it in GitHub Desktop.
Save AdamMadrzejewski/24612ef0caf79e6c4fb6 to your computer and use it in GitHub Desktop.
var parser = document.createElement('a');
parser.href = "http://example.com:3000/pathname/?search=test#hash";
parser.protocol; // => "http:"
parser.hostname; // => "example.com"
parser.port; // => "3000"
parser.pathname; // => "/pathname/"
parser.search; // => "?search=test"
parser.hash; // => "#hash"
parser.host; // => "example.com:3000"
// ES6 Style using destructuring
function getUrlParts (url) {
var magic = /^(https?):\/\/(ponyfoo\.com)(\/articles\/([a-z0-9-]+))$/;
return magic.exec(url);
}
var parts = getUrlParts('http://ponyfoo.com/articles/es6-destructuring-in-depth');
var [,protocol,host,pathname,slug] = parts;
console.log(protocol); // => 'http'
console.log(host); // => 'ponyfoo.com'
console.log(pathname); // => '/articles/es6-destructuring-in-depth'
console.log(slug); // => 'es6-destructuring-in-depth'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment