Regular Expression for accurate word-count using JavaScript

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

    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

提交回复
热议问题