Q: How do I get the word count of a string that includes HTML? I have a FreeMarker template that renders a blog post. I want to get the word count of the blog post so that I can push it to Google Tag Manager for analytics. The caveat here is that the word count cannot include the HTML characters.
I've combined the word_list and size built-ins which gets me close.
[#assign blogPost = "<h1>The Chihuahua</h1><p>The chihuahua ran across the road. Everyone thought it was a bad idea.</p><p>But he made it safely across.</p>"]

${blogPost?word_list?size}

However, I need to strip out the HTML before I count the words.

A: Remove HTML tags in Freemarker Template provides the final missing piece. Append the replace built-in with the "r" flag before the word_list and size built-ins:
${blogPost?replace("<[^>]+>", "", "r")?word_list?size}

