jquery分隔Url的param方法

时间:2021-02-03 23:18:17
最近需要分隔url的querystring,用到了特意记录一下。
方法:
//获取url中的params
var search = location.search.substring(1);
//params转换object(javascript)
search?JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g,'":"') + '"}',
              function(key, value) { return key===""?value:decodeURIComponent(value) }):{}
例子:
search = "abc=foo&def=%5Basf%5D&xyz=5&foo=b%3Dar";
function test(search)
{
  return search?JSON.parse('{"' + search.replace(/&/g, '","').replace(/=/g,'":"') + '"}', function(key, value) { return key===""?value:decodeURIComponent(value) }):{}
}
返回
Object {abc: "foo", def: "[asf]", xyz: "5", foo: "b=ar"}