又是一個小正則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[^&]+$)/, ""));
相關文章
相關標籤/搜索