Count number of words in string using JavaScript
Asked Answered
C

5

13

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.

Clack answered 1/7, 2011 at 5:26 Comment(4)
I'm not sure what your question is, but what is the check against an empty string for?Kaseykasha
in short i call function cword() with some string paragraph as an argument.but the return value is different in ff and ieClack
What's an example string that's giving you different results between browsers?Kaseykasha
when i use that much of string its give me currect result but some changes some enter ans spaces change result this below is my string Welcome to your wiki library! You can get started and add content to this page by clicking Edit at the top of this page, or you can learn more about wiki libraries by clicking [[How To Use This Library]]. What is a wiki library? Wikiwiki means quick in Hawaiian. A wiki library is a document library in which users can easily edit any page.Clack
P
3

You can make a clever use of the replace() method although you are not replacing anything.

var str = "the very long text you have...";

var counter = 0;

// lets loop through the string and count the words
str.replace(/(\b+)/g,function (a) {
   // for each word found increase the counter value by 1
   counter++;
})

alert(counter);

the regex can be improved to exclude html tags for example

Persevering answered 1/7, 2011 at 5:35 Comment(3)
i am new in javascript so plz tell me what is (a) as function argsClack
a holds the value of the current word being countedPersevering
I like that, I like that a lot. Nice Answer.Headspring
S
22

[edit 2022, based on comment] Nowadays, one would not extend the native prototype this way. A way to extend the native protype without the danger of naming conflicts is to use the es20xx symbol. Here is an example of a wordcounter using that.

Old answer: you can use split and add a wordcounter to the String prototype:

if (!String.prototype.countWords) {
  String.prototype.countWords = function() {
    return this.length && this.split(/\s+\b/).length || 0;
  };
}

console.log(`'this string has five words'.countWords() => ${
  'this string has five words'.countWords()}`);
console.log(`'this string has five words ... and counting'.countWords() => ${
  'this string has five words ... and counting'.countWords()}`);
console.log(`''.countWords() => ${''.countWords()}`);
Sumptuary answered 1/7, 2011 at 5:39 Comment(4)
When the string is empty these functions return 1.Sulk
@Sulk (because the split returns an array with the empty string indeed) adjusted the answer, thxSumptuary
🤦 Please don't modify prototypes. Remember developers.google.com/web/updates/2018/03/smooshgate#mootoolsLannielanning
@JackSteam: this answer is eleven years old. Nowadays I'd use this small library.Sumptuary
T
14

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.

Timothee answered 11/12, 2014 at 8:14 Comment(5)
This is probably the most efficient approach when dealing with long text.Antibes
Good solution, although it will generate an error if the input string is length 0 or is all whitespace.Ewing
why the group in regex? i.e. why not just /\w+/Coffman
Worked well for me! Anyone know how to avoid the 0 length error?Warrantee
@Warrantee if you're okay with optional chaining you could do: var wordCount = str.match(/(\w+)/g)?.length || 0;Katanga
C
10

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

Coffman answered 28/5, 2016 at 0:13 Comment(2)
This answer is underrated IMO.Weimer
yeah, classic stackoverflow where the actual right answer is hidden by two incomplete answers with more votes and one clearly wrong answer that is accepted as correctCoffman
P
3

You can make a clever use of the replace() method although you are not replacing anything.

var str = "the very long text you have...";

var counter = 0;

// lets loop through the string and count the words
str.replace(/(\b+)/g,function (a) {
   // for each word found increase the counter value by 1
   counter++;
})

alert(counter);

the regex can be improved to exclude html tags for example

Persevering answered 1/7, 2011 at 5:35 Comment(3)
i am new in javascript so plz tell me what is (a) as function argsClack
a holds the value of the current word being countedPersevering
I like that, I like that a lot. Nice Answer.Headspring
T
0
//Count words in a string or what appears as words :-)

        function countWordsString(string){

            var counter = 1;

            // Change multiple spaces for one space
            string=string.replace(/[\s]+/gim, ' ');

            // Lets loop through the string and count the words
            string.replace(/(\s+)/g, function (a) {
               // For each word found increase the counter value by 1
               counter++;
            });

            return counter;
        }


        var numberWords = countWordsString(string);
Tibetan answered 9/5, 2015 at 17:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.