Created
November 28, 2014 10:58
-
-
Save kkoziarski/af5badc8b294190f343d to your computer and use it in GitHub Desktop.
Parse URL parts - The Good Parts
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 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