Count repeated letters in a string

前端 未结 7 1851
耶瑟儿~
耶瑟儿~ 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:25

    This method also works well!!

    let myString = 'abababc';
    let result = {};
    for (let str of myString) {
      result[str] = result.hasOwnProperty(str) ? result[str] + 1 : 1;
    }
    console.log(result);
    

    The result will be like this {a: 3, b: 3, c: 1}

提交回复
热议问题