Count repeated letters in a string

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

    var obj = {};
    var str = "this is my string";
    for (var i = 97; i < 97 + 26; i++) 
      obj[String.fromCharCode(i)] = 0;
    for (var i = 0; i < str.length; i++) {
      obj[str.charAt(i).toLowerCase()]++;
    }
    

    From there you can say obj["a"] to get the number of occurrences for any particular letter.

提交回复
热议问题