Skip to content

Instantly share code, notes, and snippets.

@guileen
Created March 12, 2012 16:46
Show Gist options
  • Save guileen/2023265 to your computer and use it in GitHub Desktop.
Save guileen/2023265 to your computer and use it in GitHub Desktop.
simple url parser
// URL parse utility
//
// Author: Gui Lin
//
// guileen AT gmail DOT com
//
// this function is use to strip url not validation
function urlparser (url) {
var r = /^(\w+):\/\/([^\/\?#]+)(\/[^\?#]*)?(\?[^#]*)?(#.*)?$/i.exec(url);
return r && {
schema : r[1]
, domain : r[2]
, root : r[1] + '://' + r[2]
, path : r[3]
, querystring : r[4]
, anchor : r[5]
};
}
if(typeof module !== 'undefined' && module.exports) {
exports.parse = urlparser;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment