Word count after trimming white spaces in Javascript
21 Dec 2014The below function can count the number of words in a line by ignoring the white space characters .
function wordCount (str) {
if(!!str){
return str.trim().replace(/\s+/gi, ' ').split(' ').length;
}
return 0;
}
// Usage
var str = "Hello There. How is the weather today in India",
count = wordCount();
console.log(count); // 8