JS如何查找英文文章中出现频率最高的单词

这篇文章主要介绍了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;");

感谢你能够认真阅读完这篇文章,希望小编分享的“JS如何查找英文文章中出现频率最高的单词”这篇文章对大家有帮助,同时也希望大家多多支持亿速云,关注行业资讯频道,更多相关知识等着你来学习!