Skip to content

Instantly share code, notes, and snippets.

@mahfelwp
Last active September 29, 2019 18:18
Show Gist options
  • Save mahfelwp/8ba92cf2e0a26a5e703afa02d5d3b3ff to your computer and use it in GitHub Desktop.
Save mahfelwp/8ba92cf2e0a26a5e703afa02d5d3b3ff to your computer and use it in GitHub Desktop.
Simple function for work with Url in Javascript without regxp
// For more information you can see this url: https://developer.mozilla.org/en-US/docs/Web/API/URL
let parseUrl = (url) => {
let parser = document.createElement('a');
parser.href = url;
return parser;
}
let url = parseUrl("http://username:[email protected]:3000/deploy/?search=test#hash");
url.protocol; // => "http:"
url.host; // => "example.com:3000"
url.hostname; // => "example.com"
url.port; // => "3000"
url.pathname; // => "/deploy/"
url.hash; // => "#hash"
url.search; // => "?search=test"
url.origin; // => "http://example.com:3000"
url.username; // => "username"
url.password; // => "password"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment