繁体   English   中英

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

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

假设我打开了一个页面http://vedabase.net/cc/adi/9/3/en 我正在使用firefox,并在其中安装了萤火虫。 如何使用在Firebug控制台中运行的javascript在此网页中找到单词数。

尝试这个

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

对于Firefox,您可以使用(Firefox使用兼容W3C的 textContent属性。)

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

如果要摆脱空格和换行符

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

您可以使用查看字词

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

在Firefox上,代替document.body.innerText使用document.body.textContent

暂无
暂无

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

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