function test(id){
var s="2.1,1.2.1,2.1,2.1.1,2.1";
// 给前后加逗号
s = ',' + s + ',';
// 转义id里的.(点), 不然new之后就变成通配符了
id = id.replace(/\./, '\\.');
var reg = "," + id +",";
var exp = new RegExp(reg,"g");
s = s.replace(exp,",");
// 删除前后逗号
s = s.replace(/(^,)|(,$)/g, '');
alert(s);
}
test("2.1")
function test(id){
var s = '2.1,1.2.1,2.1,2.1.1,2.1';
// var reg = /^2.1,|,2.1(?=,)|,2.1$/g;
var reg = new RegExp('^' + id + ',|,' + id + '(?=,)|,' + id + '$','g');
return s.replace(reg,'');
}
alert(test('2.1'));