又是一个小正则replace
var a = "http://www.xx.com?id=111&-deb"; var b = "http://www.xx.com?-deb&id=111"; var d = "http://www.xx.com?-deb&id=111&cc=2"; var c = "http://www.xx.com?id=111"; console.log("↓把id这个字段去掉,不留多余的&和?↓"); console.log(a.replace(/\?.+/, "")); console.log(b.replace(/\?.+/, "")); console.log(c.replace(/\?.+/, "")); console.log(d.replace(/\?.+/, "")); console.log("↓只去掉id↓"); console.log(a.replace(/((id[^&]+&)|[?&]id[^&]+$)/, "")); console.log(b.replace(/((id[^&]+&)|[?&]id[^&]+$)/, "")); console.log(c.replace(/((id[^&]+&)|[?&]id[^&]+$)/, "")); console.log(d.replace(/((id[^&]+&)|[?&]id[^&]+$)/, ""));