Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of words in string using JavaScript

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 (t == undefined) {
  var total = document.getElementById('MSO_ContentTable').innerText;                
} else {
  var total = document.getElementById('MSO_ContentTable').textContent;        
}
countTotal = cword(total);   

function cword(w) {
  var count = 0;
  var words = w.split(" ");
  for (i = 0; i < words.length; i++) {
    // inner loop -- do the count
    if (words[i] != "") {
      count += 1;
    }
  }

  return (count);
}

In that code I am getting data from a div tag and sending it to the cword() function for counting. Though the return value is different in IE and Firefox. Is there any change required in the regular expression? One thing that I show that both browser send same string there is a problem inside the cword() function.

like image 945
V_B Avatar asked Jul 01 '11 05:07

V_B


People also ask

How do you count the number of words in string?

Explanation of algorithm In C++, a string acts like an array of characters, and its elements can be accessed through indexing, like in an array. The number of words is equal to the number of spaces + 1.

How do you count text in JavaScript?

In JavaScript, we can count the string occurrence in a string by counting the number of times the string present in the string. JavaScript provides a function match(), which is used to generate all the occurrences of a string in an array.

How do you find the length of a string in JavaScript?

The length of a string in JavaScript can be found using the . length property. Since . length is a property it must be called through an instance of a string class.

How do I count words in HTML?

The str_word_count() function counts the number of words in a string.


2 Answers

This is the best solution I've found:

function wordCount(str) { var m = str.match(/[^\s]+/g) return m ? m.length : 0; }

This inverts whitespace selection, which is better than \w+ because it only matches the latin alphabet and _ (see http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.2.6)

If you're not careful with whitespace matching you'll count empty strings, strings with leading and trailing whitespace, and all whitespace strings as matches while this solution handles strings like ' ', ' a\t\t!\r\n#$%() d ' correctly (if you define 'correct' as 0 and 4).

like image 82
aaron Avatar answered Oct 10 '22 23:10

aaron


I would prefer a RegEx only solution:

var str = "your long string with many words.";
var wordCount = str.match(/(\w+)/g).length;
alert(wordCount); //6

The regex is

\w+    between one and unlimited word characters
/g     greedy - don't stop after the first match

The brackets create a group around every match. So the length of all matched groups should match the word count.

like image 44
DanielH Avatar answered Oct 11 '22 00:10

DanielH