如题,我觉得难点是要匹配的 url 参数位置有多种情况:
去掉 param
:
?param=312&aa=11
→ ?aa=11
?aa=11¶m=312&bb=11
→ ?aa=11&bb=11
?aa=11¶m=312
→ ?aa=11
我只想用一句正则解决,不想分情况,有办法吗?
PHP中文网2017-04-10 15:12:48
MDN:
str.replace(regexp|substr, newSubStr|function[, flags])
// input must start with '?'
function removeParameter(str) {
return str.replace(/(^\?|&)param=[^&]*(&)?/g, function(p0, p1, p2) {
return p1 === '?' || p2 ? p1 : '';
});
}
removeParameter('?a=param=¶m=param=&b=param=param?param=');
// → ?a=param=&b=param=param?param=
removeParameter('?param=param=&a=param=&b=param=param?param=');
// → ?a=param=&b=param=param?param=
removeParameter('?a=param=&b=param=param?param=¶m=param=');
// → ?a=param=&b=param=param?param=