Skip to content

Instantly share code, notes, and snippets.

@VincentHelwig
Last active August 29, 2015 14:08
Show Gist options
  • Save VincentHelwig/4548b388dc11b25eebfa to your computer and use it in GitHub Desktop.
Save VincentHelwig/4548b388dc11b25eebfa to your computer and use it in GitHub Desktop.
Javascript Url Parsing with regexp
// FROM http://jsperf.com/url-parsing
var urlParseRE = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/;
var url = "http://jblas:password@mycompany.com:8080/mail/inbox?msg=1234&type=unread#msg-content";
urlParseRE.exec(url);
0: "http://jblas:password@mycompany.com:8080/mail/inbox?msg=1234&type=unread#msg-content"
1: "http://jblas:password@mycompany.com:8080/mail/inbox?msg=1234&type=unread"
2: "http://jblas:password@mycompany.com:8080/mail/inbox"
3: "http://jblas:password@mycompany.com:8080"
4: "http:"
5: "//"
6: "jblas:password@mycompany.com:8080"
7: "jblas:password"
8: "jblas"
9: "password"
10: "mycompany.com:8080"
11: "mycompany.com"
12: "8080"
13: "/mail/inbox"
14: "/mail/"
15: "inbox"
16: "?msg=1234&type=unread"
17: "#msg-content"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment