Using Javascript to find most common words in string?

You should split the string into words, then loop through the words and increment a counter for each one:

var wordCounts = { };
var words = str.split(/\b/);

for(var i = 0; i < words.length; i++)
    wordCounts["_" + words[i]] = (wordCounts["_" + words[i]] || 0) + 1;

The "_" + allows it to process words like constructor that are already properties of the object.

You may want to write words[i].toLowerCase() to count case-insensitively.

Leave a Comment