1var str = "your long string with many words.";
2var wordCount = str.match(/(\w+)/g).length;
3alert(wordCount); //6
4
5// \w+ between one and unlimited word characters
6// /g greedy - don't stop after the first match
1<html>
2<body>
3 <script>
4 function countWords(str) {
5 str = str.replace(/(^\s*)|(\s*$)/gi,"");
6 str = str.replace(/[ ]{2,}/gi," ");
7 str = str.replace(/\n /,"\n");
8 return str.split(' ').length;
9 }
10 document.write(countWords(" Tutorix is one of the best E-learning platforms"));
11 </script>
12</body>
13</html>