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;");

优质内容筛选与推荐>>
1、抓包工具(查看协议)
2、排序算法值归并排序
3、fio——IO基准测试
4、[LeetCode]Search in Rotated Sorted Array
5、无线客户端框架设计(3):基类的设计(iOS篇)


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号