by skymt · 14y ago · 0 comments
var wordCount = value.trim().split(/\s+/).length
Additionally, JavaScript's regex dialect has a special character of "\W", which matches any non-word character. So if you split on "\W+" instead, you ought to get a more reliable list of words, eliminating cases like "foo -- bar" counting as 3 words. Different word counting programs may give varying results,
depending on the definition of "word", on the text segmentation
algorithms, and on whether words outside the main text such as
footnotes or hidden text are counted. [1]
Anyway it seems a consensus to say that "foo -- bar" is 3 words long, so wordcount.is does give a not so wrong answer after all.However, when I tried to implement the \W, it gave me a very different count. When I checked it against Microsoft's Word's count for block of lorem ipsum text, the original function matched it exactly. Any thoughts as to what happened?
Thanks for the tips.