Skip to content

Instantly share code, notes, and snippets.

@kkoziarski
Created November 28, 2014 10:58
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 kkoziarski/af5badc8b294190f343d to your computer and use it in GitHub Desktop.
Save kkoziarski/af5badc8b294190f343d to your computer and use it in GitHub Desktop.
Parse URL parts - The Good Parts
var parse_url = /^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
var url = 'http://www.ora.com:80/goodparts?q#fragment';
var result = parse_url.exec(url);
var names = ['url', 'scheme', 'slash', 'host', 'port', 'path', 'query', 'hash'];
var blanks = ' ';
var i;
for (i = 0; i < names.length; i += 1) {
document.writeln(names[i] + ':' + blanks.substring(names[i].length), result[i]);
}
/*
url: http://www.ora.com:80/goodparts?q#fragment
scheme: http
slash: //
host: www.ora.com
port: 80
path: goodparts
query: q
hash: fragment
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment