I'd replace your function with something like this, getting rid of the regular expression for the sake of readability. It handles undefined keys/values, plus you could easily refactor the first part of it into a parseQueryString function returning a dictionary for the current document.location.search
.
function querystring(key) {
var hash = {};
var pairs = document.location.search.substr(1).split('&');
for(var i = 0; i < pairs.length; i++) {
var tuple = pairs[i].split('=');
hash[tuple[0]] = tuple[1];
}
var val = hash[key];
return (val) ? val.replace(/\+/g, ' ') : undefined;
}
Usage:
querystring('bread');
> White Bread