Skip to content

Instantly share code, notes, and snippets.

@GZShi
Created March 25, 2014 06:44
Show Gist options
  • Save GZShi/9756338 to your computer and use it in GitHub Desktop.
Save GZShi/9756338 to your computer and use it in GitHub Desktop.
REGEX: window.location.search
// version 1
function search(name) {
name = name.replace(/[\[]/g, '\\\[').replace(/[\]]/g, '\\\]');
name = encodeURIComponent(name);
var regex = new Regex('[/?&]' + name + '=([^&#]*)');
var results = regex.exec(window.location.search);
return results === null ? '' : decodeURIComponent(results[1]);
}
// version 2
(function() {
var searchObj = {};
var regex = /[/?&]([^=&#]+)=([^&#]*)/g;
var allStr = window.location.search.replace(regex, function(str, key, value) {
searchObj[decodeURIComponent(key)] = decodeURIComponent(value);
return str;
});
searchObj.toString = function(){return allStr;}
return searchObj;
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment