您好,欢迎访问一九零五行业门户网

JavaScript计算字符串中每个字符出现次数的小例子_javascript技巧

代码如下:
复制代码 代码如下:
function numinstring(str){
    str=str.replace(/ /ig,);
    var strarr=str.split();
    var result=[],beforelength,afterlength,reg;
    for(var i=0;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]
*/
其它类似信息

推荐信息