DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JavaScript計算字符串中每個字符出現次數的小例子
JavaScript計算字符串中每個字符出現次數的小例子
編輯:關於JavaScript     
代碼如下:
復制代碼 代碼如下:
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"]
*/
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved