又是一个小正则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[^&]+$)/, ""));

 

posted @ 2014-11-07 09:38  小猩猩君  阅读(220)  评论(0编辑  收藏  举报