I am trying to count the number of words in a given string using the following code:
var t = document.getElementById(\'MSO_ContentTable\').textContent;
if (
You can use split
and add a wordcounter to the String
prototype:
String.prototype.countWords = function(){
return this.split(/\s+/).length;
}
'this string has five words'.countWords(); //=> 5
If you want to exclude things like ... or - in a sentence:
String.prototype.countWords = function(){
return this.split(/\s+\b/).length;
}
'this string has seven ... words - and counting'.countWords(); //=> 7