Skip to content

Instantly share code, notes, and snippets.

@tvpmb
Created September 5, 2012 23:41
Show Gist options
  • Save tvpmb/3647795 to your computer and use it in GitHub Desktop.
Save tvpmb/3647795 to your computer and use it in GitHub Desktop.
Awesome Javascript URL Parsing
parser.href = url;
var hostname2 = parser.hostname;
var search2 = parser.search;
/*
Notes:
- extremely slow
*/
/*
Grabbed this from: http://jsperf.com/url-parsing
Referenced from this: https://gist.github.com/2428561
*/
<script>
Benchmark.prototype.setup = function() {
var urlParseRE = /^(((([^:\/#\?]+:)?(?:(\/\/)((?:(([^:@\/#\?]+)(?:\:([^:@\/#\?]+))?)@)?(([^:\/#\?\]\[]+|\[[^\/\]@#?]+\])(?:\:([0-9]+))?))?)?)?((\/?(?:[^\/\?#]+\/+)*)([^\?#]*)))?(\?[^#]+)?)(#.*)?/;
var parser = document.createElement('a');
var url = "http://jblas:[email protected]:8080/mail/inbox?msg=1234&type=unread#msg-content";
};
</script>
var matches = urlParseRE.exec(url);
var hostname1 = matches[11];
var search1 = matches[16];
/*
Notes:
- extremely fast
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment