Regular Expression for accurate word-count using JavaScript

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

    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
    

提交回复
热议问题