下面这个函数是js查找一篇英文文章中出现频率最高的单词(由26个英文字母大小写构成),输出该单词及出现次数,不区分大小写,主要是正则的运用:
rush:js;">
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;");
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程之家。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。