Skip to content

Instantly share code, notes, and snippets.

@balanza
Created January 27, 2015 15:20
Show Gist options
  • Save balanza/264f3f1c1eb362c3a7ce to your computer and use it in GitHub Desktop.
Save balanza/264f3f1c1eb362c3a7ce to your computer and use it in GitHub Desktop.
Parse URL (including HTTP validation)
var parseUrl = function(url) {
var r = /^((http[s]?|ftp):\/)?\/?(([\w\-\.]+):([\w\-\.\$]+)@)?([^:\/\s]+)((\/\w+)*?(:([0-9]+))\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$/;
var tmp = r.exec(url);
return {
original: tmp[0],
protocol: tmp[2],
username: tmp[4],
password: tmp[5],
host: tmp[6],
port: tmp[10],
path: tmp[11],
query: tmp[12]
};
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment