码迷,mamicode.com
首页 > Web开发 > 详细

js查找一篇英文文章中出现频率最高的单词

时间:2015-05-29 21:45:25      阅读:281      评论:0      收藏:0      [点我收藏+]

标签:

下面这个函数是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查找一篇英文文章中出现频率最高的单词

标签:

原文地址:http://www.cnblogs.com/byronic/p/4539416.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!