Last active
April 17, 2018 01:11
-
-
Save ansiz/1575e41509a370b003fbe8b7ac1221c7 to your computer and use it in GitHub Desktop.
JavaScript URI parser in regular expressions
This file contains 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
function parseURI(url) { | |
var m = String(url).replace(/^\s+|\s+$/g, '').match(/^([^:\/?#]+:)?(\/\/(?:[^:@]*(?::[^:@]*)?@)?(([^:\/?#]*)(?::(\d*))?))?([^?#]*)(\?[^#]*)?(#[\s\S]*)?/); | |
// authority = '//' + user + ':' + pass '@' + hostname + ':' port | |
return (m ? { | |
href: m[0] || '', | |
protocol: m[1] || '', | |
authority: m[2] || '', | |
host: m[3] || '', | |
hostname: m[4] || '', | |
port: m[5] || '', | |
pathname: m[6] || '', | |
search: m[7] || '', | |
hash: m[8] || '' | |
} : null); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment