Skip to content

Instantly share code, notes, and snippets.

@armincifuentes
Created March 8, 2017 20:07
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 armincifuentes/bd1a06913e535c6e1d6a663b15c4d6e1 to your computer and use it in GitHub Desktop.
Save armincifuentes/bd1a06913e535c6e1d6a663b15c4d6e1 to your computer and use it in GitHub Desktop.
Count words in a HTML text body
function wordCount(text) {
return text
.replace(/[–,;.:!¡¿\-\?\(\)]/g, '') // remove punctuation
.replace(/<[^>]+>/g, ' ') // convert tags into whitespaces
.split(/\s/) // split across whitespaces
.filter(word => word.length) // remove empty keys
.length;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment