match實現 getParam();
var str = 'http://www.zhufengpeixun.cn/?lx=1&from=wx&b=12&c=13';
function getParam(url) {
var reg = /([^?=&]+)=([^?=&
var obj = {};
url.match(reg).forEach(item => { //match分割直接是大正則內容["lx=1", "from=wx", "b=12", "c=13"]
let a = item.split('=');//須要再以=切割一下變成:["lx", "1"]
obj[a[0]] = a[1]//直接拿索引0和索引1就能夠了
})
let v = url.match(/
obj.hash = v;
return obj;
}
console.log(getParam(str));//{lx: "1", from: "wx", b: "12", c: "13"}
replace 實現1getParam();
var str = 'http://www.zhufengpeixun.cn/?lx=1&from=wx&b=12&c=13';
function getParam(url) {
var reg = /([^?=&]+)=([^?=&
let obj ={};
var res = str.replace(reg,function($0,$1,$2){//$0表明大正則內容 $1表明第一個分組 $2表明第二個分組
console.log($0,$1,$2);//lx=1 lx 1
obj[$1] = $2;
})
let v = url.match(/
obj.hash = v;
return obj;
}
console.log(getParam(str));//{lx: "1", from: "wx", b: "12", c: "13"}
複製代碼