Skip to content

Instantly share code, notes, and snippets.

@ansiz
Last active April 17, 2018 01:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ansiz/1575e41509a370b003fbe8b7ac1221c7 to your computer and use it in GitHub Desktop.
Save ansiz/1575e41509a370b003fbe8b7ac1221c7 to your computer and use it in GitHub Desktop.
JavaScript URI parser in regular expressions
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