用JavaScript计数字 [英] Count words with JavaScript
本文介绍了用JavaScript计数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是一个新的编码,所以我想知道如何可以用javascript来计算网站的字。我应该使用 .innerText
还是代替
I am new to coding, so I would like to know how I can count words of an website with javascript. Should I use .innerText
and a for
loop?
推荐答案
拆分正则表达式 / \ W + /
( \W
匹配任何不是拉丁字母或阿拉伯数字或下划线的内容):
Split on regular expression /\W+/
(\W
matches anything that is not a latin letter or arabic number or an underscore) :
var text = "These are two sentences. They have ten words in total.";
alert(text.split(/\W+/).length)
有关regexp的更多细节可以在MDN上找到:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
More details on regexp can by found on MDN: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp
这篇关于用JavaScript计数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文