Skip to content

Instantly share code, notes, and snippets.

@vvtommy
Forked from metafeather/URL parsing Regex.js
Created July 9, 2018 15:02
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 vvtommy/4434eb2d8f17e47f6a6d1fc18c0b4f41 to your computer and use it in GitHub Desktop.
Save vvtommy/4434eb2d8f17e47f6a6d1fc18c0b4f41 to your computer and use it in GitHub Desktop.
URL parsing regex.js
/*
A single regex to parse and breakup a full URL including query parameters and anchors e.g.
https://www.google.com/dir/1/2/search.html?arg=0-a&arg1=1-b&arg3-c#hash
*/
Url.regex = /^((http[s]?|ftp):\/)?\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$/;
url: RegExp['$&'],
protocol: RegExp.$2,
host: RegExp.$3,
path: RegExp.$4,
file: RegExp.$6,
query: RegExp.$7,
hash: RegExp.$8
/*
Alternate from Reverse HTTP javascript server http://www.reversehttp.net/demos/httpd.js
*/
Url.regex =
/*12 3 45 6 7 8 9 A B C D E F 0 */
/* proto user pass host port path query frag */
/^((\w+):)?(\/\/((\w+)?(:(\w+))?@)?([^\/\?:]+)(:(\d+))?)?(\/?([^\/\?#][^\?#]*)?)?(\?([^#]+))?(#(\w*))?/;
this.url = r[0];
this.protocol = r[2];
this.username = r[5];
this.password = r[7];
this.host = r[8] || "";
this.port = r[10];
this.pathname = r[11] || "";
this.querystring = r[14] || "";
this.fragment = r[16] || "";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment