Regular Expression for accurate word-count using JavaScript

后端 未结 7 1017
陌清茗
陌清茗 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:21

    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
    

提交回复
热议问题