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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。

时间: 2017-03-17

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值