我正在寻找一种方法来识别页面上的所有单词,并计算该页面上每个单词的每个实例的数量。我需要为此使用 JavaScript,而不是 jQuery。
更新
这是我到目前为止所拥有的,虽然它似乎正在工作,但我仍然遇到一些将 2 个或更多单词合并在一起的情况,有什么线索吗?
if(window.attachEvent) {
window.attachEvent("onload", myFunc);
} else {
if(window.onload) {
var curronload = window.onload;
var newonload = function() {
curronload();
myFunc();
};
window.onload = newonload;
} else {
window.onload = myFunc;
}
}
function myFunc() {
var words = document.body.innerText;
words = words.replace(/\n/g, " "); //Remove line breaks
words = words.split(" ");
var foundWords = new Array();
var counts = new Array();
words.forEach(function(s) {
s = s.replace(/^\s+|\s+$/g,''); //Trim
s = s.toLowerCase(); //To lower case
var index = foundWords.indexOf(s);
if(s != \'\') { //If word not blank
if(index < 0) {
foundWords.push(s);
var newindex = foundWords.indexOf(s);
counts.push(1);
} else {
counts[index] += 1;
}
}
});
//Cycle through all found words and log the index, word & count
foundWords.forEach( function(s) {
var index = foundWords.indexOf(s);
console.log(index+" "+s+" "+counts[index]);
});
}