-
-
Save AdamMadrzejewski/24612ef0caf79e6c4fb6 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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