Count repeated letters in a string

前端 未结 7 1991
耶瑟儿~
耶瑟儿~ 2021-01-06 02:06

I\'m stuck with the following problem: I need to find repeated characters in a string. Basically what I want is regular expression that will match like that



        
7条回答
  •  清歌不尽
    2021-01-06 02:22

    var re = /([a-z])(?:.*)(\1)+/g; 
    var str = ['aaaccbcdd'];
    var m;
    var result = new Array();
    
    for(var i = 0; i < str.length; i++) {
      result[i] = str[i] + "->";
      while ((m = re.exec(str[i])) !== null) {
          if (m.index === re.lastIndex) {
              re.lastIndex++;
          }
          // View your result using the m-variable.
          // eg m[0] etc.
        result[i] += m[1];
        result[i] += m[2] + ",";
      }
    }
    
    document.getElementById("results").innerHTML = result.join("
    ");

提交回复
热议问题