Skip to content

Instantly share code, notes, and snippets.

@ydhao
Created April 18, 2018 07:21
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 ydhao/9e892f23623dde9ca99ddf488d4ad893 to your computer and use it in GitHub Desktop.
Save ydhao/9e892f23623dde9ca99ddf488d4ad893 to your computer and use it in GitHub Desktop.
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