function findMostWord(article) {
// 判断合法性
if (!article) return;
// 参数处理
article = article.trim().toLowerCase();
let wordList = article.match(/[a-z]+/g),
visited = [],
maxNum = 0,
maxWord = "";
console.log(wordList)
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;
}
}
});
return maxWord + " " + maxNum;
}
console.log(findMostWord("I am sam say yes yes yes"))
console