使用 JavaScript 进行精确字数统计的正则表达式
我正在尝试为 JavaScript 命令编写一个正则表达式,以准确计算文本区域中的单词数。
我发现的一种解决方案如下:
document.querySelector("#wordcount").innerHTML = document.querySelector("#editor").value.split(/\b\w+\b/).length -1;
但这不计算任何非拉丁字符(例如:西里尔文、韩文等);它完全跳过它们。
我放在一起的另一个:
document.querySelector("#wordcount").innerHTML = document.querySelector("#editor").value.split(/\s+/g).length -1;
但这并不准确,除非文档以空格字符结尾。如果将空格字符附加到正在计数的值后,即使文档为空,也会将其计为 1 个单词。此外,如果文档以空格字符开头,则会计算无关单词。
是否有一个正则表达式可以放入此命令中,无论输入法如何,都可以准确地计算单词数?
I'm trying to put together a regular expression for a JavaScript command that accurately counts the number of words in a textarea.
One solution I had found is as follows:
document.querySelector("#wordcount").innerHTML = document.querySelector("#editor").value.split(/\b\w+\b/).length -1;
But this doesn't count any non-Latin characters (eg: Cyrillic, Hangul, etc); it skips over them completely.
Another one I put together:
document.querySelector("#wordcount").innerHTML = document.querySelector("#editor").value.split(/\s+/g).length -1;
But this doesn't count accurately unless the document ends in a space character. If a space character is appended to the value being counted it counts 1 word even with an empty document. Furthermore, if the document begins with a space character an extraneous word is counted.
Is there a regular expression I can put into this command that counts the words accurately, regardless of input method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试计算任何非空白且带有字边界的内容:
您也可以尝试使用 unicode 范围,但我不确定以下范围是否完整:
Try to count anything that is not whitespace and with a word boundary:
You could also try to use unicode ranges, but I am not sure if the following one is complete:
尝试
这将匹配可以出现在单词中的字符串。而类似:
如果用户添加逗号或其他后面没有空格的标点符号,或者添加逗号两侧都有空格,则会导致计数不正确。
Try
This will match a string of characters that can be in a word. Whereas something like:
will result in an incorrect count if the user adds commas or other punctuation that is not followed by a space - or adds a comma with a space either side of it.
对我来说,这给出了最好的结果:
有了
你就得到了所有的话语。
说明:
我建议学习正则表达式。这是一项很棒的技能,因为它们非常强大。 ;-)
For me this gave the best results:
with
you get all words.
Explanation:
I recommend learning regular expressions. It's a great skill to have because they are so powerful. ;-)
您可以扩展/更改这样的方法
document.querySelector("#wordcount").innerHTML = document.querySelector("#editor").value.split(/\b\(.*?) \b/).length -1;
如果您还想匹配电子邮件地址等内容,则document.querySelector("#wordcount").innerHTML = document.querySelector(" #editor").value.trim().split(/\s+/g).length -1;
也尝试使用
\s
作为其\w
对于 unicode源:http://www.regular-expressions.info/charclass.html
you could extend/change you methods like this
document.querySelector("#wordcount").innerHTML = document.querySelector("#editor").value.split(/\b\(.*?)\b/).length -1;
if you want to match things like email-addresses as welland
document.querySelector("#wordcount").innerHTML = document.querySelector("#editor").value.trim().split(/\s+/g).length -1;
also try using
\s
as its the\w
for unicodesource:http://www.regular-expressions.info/charclass.html
正确的正则表达式是
/s+/
以丢弃非单词:The correct regexp would be
/s+/
in order to discard non-words:我的简单 JavaScript 库,名为 FuncJS,有一个名为“count()”的函数,它的作用正是它的名字——计算单词数。
例如,假设您有一个充满单词的字符串,您可以简单地将其放在函数括号之间,如下所示:
然后调用该函数,该函数将返回单词数。此外,该函数旨在忽略任何数量的空格,从而给出准确的结果。
要了解有关此函数的更多信息,请阅读文档 http://docs.funcjs.webege .com/count().html 并且 FuncJS 的下载链接也在页面上。
希望这可以帮助任何想要这样做的人! :)
my simple JavaScript library, called FuncJS has a function called "count()" which does exactly what it's called — count words.
For example, say that you have a string full of words, you can simply place it in between the function brackets, like this:
and then call the function, which will then return the number of words. Also, this function is designed to ignore any amount of whitespace, thus giving an accurate result.
To learn more about this function, please read the documentation at http://docs.funcjs.webege.com/count().html and the download link for FuncJS is also on the page.
Hope this helps anyone wanting to do this! :)
这应该做你想做的事:
你不是分割字符串,而是匹配任何非空白字符序列。
如果需要的话,还有一个额外的好处是可以轻松提取每个单词;)
This should do what you're after:
Rather than splitting the string, you're matching on any sequence of non-whitespace characters.
There's the added bonus of being easily able to extract each word if needed ;)