Javascript 如何找到一页上的所有单词,以及每个单词有多少个
我正在寻找一种方法来识别页面上的所有单词,并计算该页面上每个单词的每个实例的数量。不过,我需要使用JavaScript,而不是jQuery 更新 这就是我到目前为止所拥有的,虽然它似乎是有效的,但我仍然得到一些案例,其中2个或更多的单词被合并在一起,有任何线索吗Javascript 如何找到一页上的所有单词,以及每个单词有多少个,javascript,Javascript,我正在寻找一种方法来识别页面上的所有单词,并计算该页面上每个单词的每个实例的数量。不过,我需要使用JavaScript,而不是jQuery 更新 这就是我到目前为止所拥有的,虽然它似乎是有效的,但我仍然得到一些案例,其中2个或更多的单词被合并在一起,有任何线索吗 if(window.attachEvent) { window.attachEvent("onload", myFunc); } else { if(window.onload) { var curron
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]);
});
}
if(window.attachEvent){
window.attachEvent(“onload”,myFunc);
}否则{
if(窗口加载){
var curronload=window.onload;
var newonload=函数(){
currenload();
myFunc();
};
window.onload=newonload;
}否则{
window.onload=myFunc;
}
}
函数myFunc(){
var words=document.body.innerText;
words=words.replace(//\n/g,“”;//删除换行符
单词=单词。拆分(“”);
var foundWords=新数组();
变量计数=新数组();
words.forEach(函数){
s=s.replace(//^\s+|\s+$/g',);//修剪
s=s.toLowerCase();//小写
var索引=foundWords.indexOf(s);
if(s!=\'\'){//if单词非空
如果(指数<0){
发现单词。推;
var newindex=foundWords.indexOf(s);
计数。推(1);
}否则{
计数[指数]+=1;
}
}
});
//循环浏览所有找到的单词,并记录索引、单词和计数
foundWords.forEach(函数{
var索引=foundWords.indexOf(s);
log(索引+“”+s+“”+计数[索引]);
});
}
像这样使用正则表达式
var words = document.body.textContent || document.body.innerText,
matches = words.match(/word/gmi);
console.log(matches);
var findWord="What";
var totalCount = document.body.innerText.split(findWord).length - 1;
你可以这样用
var words = document.body.textContent || document.body.innerText,
matches = words.match(/word/gmi);
console.log(matches);
var findWord="What";
var totalCount = document.body.innerText.split(findWord).length - 1;
您可以润色此解决方案:
<!DOCTYPE html>
<html>
<body>
<p id="demo">Click the button to display the matches.</p>
<button onclick="myFunction()">Try it</button>
<script>
function myFunction()
{
var str="The rain in SPAIN stays mainly in the plain rain";
var n=str.match(/\S+/g);
document.getElementById("demo").innerHTML=n;
for(i=0; i < n.length ; i++){
r = str.match(new RegExp( n[i], 'g' ));
document.getElementById("demo").innerHTML+= '<br>'+ n[i] +' = ' + r.length ;
}
}
</script>
</body>
</html>
单击按钮以显示匹配项
试试看
函数myFunction()
{
var str=“西班牙的雨主要集中在平原雨”;
var n=str.match(/\S+/g);
document.getElementById(“demo”).innerHTML=n;
对于(i=0;i'+n[i]+'='+r.length;
}
}
我必须承认,我同意(有些讽刺的)评论员们的观点,他们敦促首先对基本JavaScript进行一些研究。不过,我觉得尝试一下会很有趣,所以我想到了第一件事
它将单词列表和频率输出到控制台
当然,有人会想过滤结果,使其更好一些,但这是另一个问题
var-words=[];
var walkDOM=函数(节点,func){
func(节点);
node=node.firstChild;
while(节点){
walkDOM(node,func);
node=node.nextSibling;
}
};
walkDOM(document.body,function(node){
如果(node.nodeName=='#text'){
var text=node.textContent;
text=text.替换(/[^A-Za-z]/g');
text=text.split(“”);
如果(文本长度){
对于(变量i=0,长度=text.length;i
这使用了Douglas Crockford在JavaScript中的walkDOM示例:好的部分。但我从其他人那里看到document.body有一个innerText属性?!那就简单多了
我留下这个答案是因为保持字数的方法可能会对提问者有用。目前为止没有什么,javascript不是我的强项,我不知道我首先应该如何在javascript中做到这一点1)选择所有文本节点2)将文本拆分为单词3)计算每个单词的出现次数4)打印结果;阅读了一篇像样的Javascript教程后,你需要哪一部分呢?3a)对单词列表进行排序3b)扫描列表以查找列表中相邻的相同项目3c)将计数存储在单词this works的某个位置,类似这样。不过,我得到了大量javascript和一些div元素。为了进行测试,我在这个页面上运行了:javascript:console.log(document.body.textContent),我使用innerText获得了更好的结果。document.body.innerText是否可以在所有浏览器(包括IE)上运行@user1448020不确定,但您可以在Google上搜索兼容性。一旦我有了纯文本字符串,如何从中提取所有单词?我不是在寻找一个或多个特定的单词,我只是想找出页面上最常用的单词,而不是特定的单词本身。一旦我有了数组中的所有单词,我应该很好,我可以运行数组并计算每个单词的实例。使用
split(“”
并计算每个单词的出现次数如何从列表中找到最频繁的单词?只需按出现次数对结果排序:words=words.sort((a,b)=>b[1]-a[1])代码>()