正则表达式提取url中的参数,返回json字符串

时间:2021-09-26 20:15:27
var urlstr = "www.baidu.com?a=1&b=xx&c";
var s = urlstr.split("?");
var argstr="";
console.log(s);
if(s.length>1){
    argstr = s[1];
}
var parttern = /([a-z]+)(=)?([a-z0-9]*)/gi;

var matches = parttern.exec(argstr);
var arraymatches = [];
while(matches){
    arraymatches.push(matches);
    matches = parttern.exec(argstr);
}
console.log(arraymatches);
var jsonop = {};
for(var i=0;i<arraymatches.length;i++){
    var temp = arraymatches[i];
    jsonop[temp[1]] = temp[3];
}
console.log(jsonop);
console.log( JSON.stringify(jsonop));