Regular Expression for accurate word-count using JavaScript

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


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

For me this gave the best results:

value.split(/\b\W+\b/).length

with

var words = value.split(/\b\W+\b/)

you get all words.

Explanation:

  • \b is a word boundary
  • \W is a NON-word character, capital usually means the negation
  • '+' means 1 or more characters or the prefixed character class

I recommend learning regular expressions. It's a great skill to have because they are so powerful. ;-)


Try

    value.match(/\w+/g).length;

This will match a string of characters that can be in a word. Whereas something like:

    value.match(/\S+/g).length;

will result in an incorrect count if the user adds commas or other punctuation that is not followed by a space - or adds a comma with a space either side of it.