这篇文章介绍了在JS中计算字符串中每个字符出现的次数
有需要的朋友可以参考一下
代码如下:
复制代码 代码如下:
function numInstring(str){
str=strreplace(/ /ig"");
var strArr=strsplit("");
var result=[]beforeLengthafterLengthreg;
for(var i=;i<strArrlength;i++){
if(strindexOf(strArr[i])!=){
beforeLength=strlength;
reg=new RegExp(strArr[i]"ig");
str=strreplace(reg"");
afterLength=strlength;
resultpush(strArr[i]+":"+(beforeLengthafterLength));
}
}
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");
consolelog(result);
/*result:
["T:" "h:" "i:" "s:" "e:" "c:" "o:" "n:" "f:" "d:" "a:" "J:" "v:" "r:" "p:" "l:" "g:" "u:" ":" "W:" "b:"]
*/