使用 JavaScript 进行精确字数统计的正则表达式

发布于 2024-10-10 22:02:53 字数 557 浏览 2 评论 0原文

我正在尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

丢了幸福的猪 2024-10-17 22:02:54

尝试计算任何非空白且带有字边界的内容:

value.split(/\b\S+\b/g).length

您也可以尝试使用 unicode 范围,但我不确定以下范围是否完整:

value.split(/[\u0080-\uFFFF\w]+/g).length

Try to count anything that is not whitespace and with a word boundary:

value.split(/\b\S+\b/g).length

You could also try to use unicode ranges, but I am not sure if the following one is complete:

value.split(/[\u0080-\uFFFF\w]+/g).length
小耗子 2024-10-17 22:02:54

尝试

    value.match(/\w+/g).length;

这将匹配可以出现在单词中的字符串。而类似:

    value.match(/\S+/g).length;

如果用户添加逗号或其他后面没有空格的标点符号,或者添加逗号两侧都有空格,则会导致计数不正确。

Try

    value.match(/\w+/g).length;

This will match a string of characters that can be in a word. Whereas something like:

    value.match(/\S+/g).length;

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.

蔚蓝源自深海 2024-10-17 22:02:54

对我来说,这给出了最好的结果:

value.split(/\b\W+\b/).length

有了

var words = value.split(/\b\W+\b/)

你就得到了所有的话语。

说明:

  • \b 是单词边界
  • \W 是非单词字符,大写通常表示否定
  • '+' 表示 1 个或多个字符或前缀字符类

我建议学习正则表达式。这是一项很棒的技能,因为它们非常强大。 ;-)

For me this gave the best results:

value.split(/\b\W+\b/).length

with

var words = value.split(/\b\W+\b/)

you get all words.

Explanation:

  • \b is a word boundary
  • \W is a NON-word character, capital usually means the negation
  • '+' means 1 or more characters or the prefixed character class

I recommend learning regular expressions. It's a great skill to have because they are so powerful. ;-)

尬尬 2024-10-17 22:02:54

您可以扩展/更改这样的方法

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 well

and

document.querySelector("#wordcount").innerHTML = document.querySelector("#editor").value.trim().split(/\s+/g).length -1;

also try using \s as its the \w for unicode

source:http://www.regular-expressions.info/charclass.html

偏爱自由 2024-10-17 22:02:54

正确的正则表达式是 /s+/ 以丢弃非单词:

'Lorem ipsum dolor , sit amet'.split(/\S+/g).length
7
'Lorem ipsum dolor , sit amet'.split(/\s+/g).length
6

The correct regexp would be /s+/ in order to discard non-words:

'Lorem ipsum dolor , sit amet'.split(/\S+/g).length
7
'Lorem ipsum dolor , sit amet'.split(/\s+/g).length
6
双马尾 2024-10-17 22:02:54

我的简单 JavaScript 库,名为 FuncJS,有一个名为“count()”的函数,它的作用正是它的名字——计算单词数。

例如,假设您有一个充满单词的字符串,您可以简单地将其放在函数括号之间,如下所示:

count("How many words are in this string?");

然后调用该函数,该函数将返回单词数。此外,该函数旨在忽略任何数量的空格,从而给出准确的结果。

要了解有关此函数的更多信息,请阅读文档 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:

count("How many words are in this string?");

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! :)

只是在用心讲痛 2024-10-17 22:02:54
const wordsCount = str.match(/\p{L}+/gu).length
const wordsCount = str.match(/\p{L}+/gu).length
╰沐子 2024-10-17 22:02:53

这应该做你想做的事:

value.match(/\S+/g).length;

你不是分割字符串,而是匹配任何非空白字符序列。

如果需要的话,还有一个额外的好处是可以轻松提取每个单词;)

This should do what you're after:

value.match(/\S+/g).length;

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 ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文