js字符串去除连续重复字符
()和\number 配合使用表示重复正则第number个括号内匹配到的内容
,如:(\d)\1表示重复第一个匹配块(\d)即等价于如果(\d)匹配到a,则表达式为aa
var s = "1122333455";
var s1 = s;
var c;
//11,22,333,55 当然这里用()\1*也会可以(因为下面是替换):11,22,333,4,55
var cc = s.match(/(\d)\1+/g);
for(var i = 0;i<cc.length;i++)
{
c = cc[i].substring(0,1);
s1 = s1.replace(cc[i],c);
}
alert(s1); //12345
js字符串去除全部重复字符,并把最终字符串排序
var s = "1234321abaccc";
var s1 = s.split("").sort().join("");
var cc = s1.match(/(.)\1+/g);
//11,22,33,aa,ccc 当然这里用()\1*也会可以(因为下面是替换):11,22,33,4,aa,b,ccc
for(var i = 0;i<cc.length;i++){
c = cc[i].substring(0,1);
s1 = s1.replace(cc[i],c);
}
alert(s1); //1234abc