I see that you're replacing <br /> but from what I see on the page, there is <br> too.
You can deal with that issue by instead using:
Code:
.replace(/<br[ \/]*>/g, ' ')
If you now look at the resulting text after it's split by a space, you'll find that you have:
Code:
["
Alpha", "Bravo
Charlie
Delta
Echo", "Foxtrot", "Gamma
"]
The reason for this is that the newline characters aren't being split up. You can fix that by splitting using /\s/ instead, which is a regular expression for a white-space separator.
When you do that you'll then have:
Code:
["", "Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Gamma", ""]
So you need to trim things off first, before splitting it.
Here's a way of doing it, where you get the text first, and then rework it for the words.
Code javascript:
var text = $(this).html().replace(/<blockquote>(.*?)<\/blockquote>/g, '').replace(/<div class="spoiler_toggle">(.*?)<\/div>/g, '').replace(/<div class="spoiler">(.*?)<\/div>/g, '').replace(/<br[ \/]*>/g, ' '),
words = $.trim(text).split(/\s/).length;
That will give you a result of 7 words, which is correct for the example.
But what happens though when there are multiple spaces between words, your word count will then be off again.
You can deal with that by using the + symbol to have the split capture one or more pieces of white-space:
Which leaves us with:
Code javascript:
var text = $(this).html().replace(/<blockquote>(.*?)<\/blockquote>/g, '').replace(/<div class="spoiler_toggle">(.*?)<\/div>/g, '').replace(/<div class="spoiler">(.*?)<\/div>/g, '').replace(/<br[ \/]*>/g, ' '),
words = $.trim(text).split(/\s+/).length;
Bookmarks