js查找英文文章中出现的次数|JS查找英文文章中出现频率最高的单词

更新时间:2021-07-30    来源:php函数    手机版     字体:

【www.bbyears.com--php函数】

下面这个函数是js查找一篇英文文章中出现频率最高的单词(由26个英文字母大小写构成),输出该单词及出现次数,不区分大小写,主要是正则的运用:

 

 代码如下

functioncounts(article){

 article = article.trim().toUpperCase();

 vararray = article.match(/[A-z]+/g);

 article =" "+array.join(" ")+" ";

 varmax = 0,word,num = 0,maxword="";

 for(vari = 0; i < array.length; i++) { 

  word =newRegExp(" "+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;");

 

本文来源:http://www.bbyears.com/jiaocheng/132833.html