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;");
function findMostWord(article) { // 合法性判断 if (!article) return;
// 参数处理 article = article.trim().toLowerCase();
let wordList = article.match(/[a-z]+/g), visited = [], maxNum = 0, maxWord = "";
article = " " + wordList.join(" ") + " ";
// 遍历判断单词出现次数 wordList.forEach(function(item) { if (visited.indexOf(item) < 0) { let word = new RegExp(" " + item + " ", "g"), num = article.match(word).length;
if (num > maxNum) {
maxNum = num;
maxWord = item;
}
}
});
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。