Skip to content

Instantly share code, notes, and snippets.

@ariews
Created March 13, 2013 15:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariews/5153098 to your computer and use it in GitHub Desktop.
Save ariews/5153098 to your computer and use it in GitHub Desktop.
/**
* Takes the given HTML data, replaces all its HTML tags with nothing, splits the result by spaces,
* and outputs the array length i.e. number of words.
*
* @param string htmlData HTML Data
* @return int Word Count
*/
function GetWordCount(htmlData) {
var count = 0;
if (htmlData != '') {
htmlData = $(htmlData.replace(/[\s\n\t]+/g, ' ')).text();
htmlData = htmlData.replace(/\s+/g, ' ').split(' ');
$(htmlData).each(function(index){
if (this != '') count += 1;
});
}
return count;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment