简体   繁体   English

使用javascript计算网页中的单词数

[英]count the number of words in a web page using javascript

Lets suppose i opened a page http://vedabase.net/cc/adi/9/3/en . 假设我打开了一个页面http://vedabase.net/cc/adi/9/3/en I am using firefox and have firebug installed in it. 我正在使用firefox,并在其中安装了萤火虫。 How can i find the number of word in this web page using javascript running in the console of firebug. 如何使用在Firebug控制台中运行的javascript在此网页中找到单词数。

try this 尝试这个

document.body.innerText.split(' ').length

for Firefox you can use (Firefox uses the W3C-compliant textContent property.) 对于Firefox,您可以使用(Firefox使用兼容W3C的 textContent属性。)

document.body.textContent.split(' ').length

If you want to get rid of the blank spaces and newlines 如果要摆脱空格和换行符

document.body.innerText.split(/\s/).filter(function (txt) {return /\S/.test(txt)}).length;

You can see the words using 您可以使用查看字词

document.body.innerText.split(/\s/).filter(function (txt) {return /\S/.test(txt)});

On firefox, instead of document.body.innerText use document.body.textContent 在Firefox上,代替document.body.innerText使用document.body.textContent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM