I got this code for counting the number of words from an html editor.
(providing htmlData has already been set)
var rawWords = htmlData.replace(/<(?:.|\s)*?>/g, '')
.replace(/(\r\n|\n|\r)/gm,' ');
var filteredWords = rawWords.replace(/\[([^\]]+)\]/g,'')
.replace(/\s+/g, " ")
.replace(/^\s+|\s+$/g, "");
From what I understand, the first line removes the html and then removes any returns.
The next line removes anything in brackets (this is to add notes without affecting the word count) and then removes extra spaces
But if I type this:
Apple
Charlie
Tom
It gives me a word count of 6, not 3. Any idea why? I’m not good at regex!!!!
thanks so much
Try this, it’s simple, just splits the whitespace/numbers, and counts the array.
full example below: