Regular Expression for accurate word-count using JavaScript

后端 未结 7 1004
陌清茗
陌清茗 2020-12-01 07:15

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 fo

7条回答
  •  渐次进展
    2020-12-01 07:16

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

提交回复
热议问题