This article introduces the number of occurrences of each character in a string in JS, and a friend in Need can refer to
The code is as follows:
Copy Code code as follows:
Function numinstring (str) {
str=str.replace (//ig, "");
var Strarr =str.split ("");
var Result=[],beforelength,afterlength,reg;
for (var i=0;i<strarr.length;i++) {
if ( Str.indexof (Strarr[i])!=-1) {
beforelength =str.length;
reg=new RegExp (strarr[i], "IG");
str=str.replace (Reg, "");
afterlength=str.length;
Result.push (strarr[i]+ ":" + ( Beforelength-afterlength));
}
}
return result;
}
Var result=numinstring("This section of the site is dedicated to the JavaScript language itself, the parts that are not specific to WEB pages or Other host ");
Console.log (Result);
/*result:
["T:17", "H:7", "I:9", "s:10", "e:14", "C:5", "O:8", "N:3", "F:3", "D:3", "A:9", "J:1", "V:1", "R:5", " P:4 "," L:2 "," G:3 "," u:1 ",",: 1 "," w:1 "," b:1 "]
*/