Created
March 25, 2014 06:44
-
-
Save GZShi/9756338 to your computer and use it in GitHub Desktop.
REGEX: window.location.search
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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