深色模式
查找文章中出现最多的单词
js
function findMostWord(str) {
if (!str) return str;
str = str.trim();
let regex = /[a-z]|+/g;
let words = str.match(regex);
let visited = [];
let maxnum = 0;
let maxword = '';
str = ' ' + words.join(' ') + ' ';
words.forEach((item) => {
if (visited.indexOf(item) === -1) {
let wordregex = ` ${item} `;
let length = str.match(wordregex, g).length;
if (length > maxnum) {
maxnum = length;
maxword = item;
}
}
});
return maxword;
}