一、JS查找一篇英文文章中出現頻率最高的單詞
下面這個函數是js查找一篇英文文章中出現頻率最高的單詞(由26個英文字母大小寫構成),輸出該單詞及出現次數,不區分大小寫,主要是正則的運用:
function counts(article){
article = article.trim().toUpperCase();
var array = article.match(/[A-z]+/g);
article = " "+array.join(" ")+" ";
var max = 0,word,num = 0,maxword="";
for(var i = 0; i < array.length; i++) {
word = new RegExp(" "+array[i]+" ",'g');
num = article.match(word).length;
if(num>max){
max=num;
maxword = array[i];
}
}
console.log(maxword+" "+max);
}
counts("Age has reached the end of the beginning of a word. May be guilty in his seems to passing a lot of different life became the appearance of the same day;");