Regex remove repeated characters from a string by javascript

前端 未结 2 844
别跟我提以往
别跟我提以往 2020-12-05 19:13

I have found a way to remove repeated characters from a string using regular expressions.

function RemoveDuplicates() {
    var str = \"aaabbbccc\";
    var          


        
2条回答
  •  死守一世寂寞
    2020-12-05 19:33

    This is an old question, but in ES6 we can use Sets. The code looks like this:

    var test = 'aaabbbcccaabbbcccaaaaaaaasa';
    var result = Array.from(new Set(test)).join('');
    
    console.log(result);

提交回复
热议问题