I have a word counter function but it doesn't account for people using poor punctuation, for example:
"hello.world"
That would only count is as 1 word. Instead it should count that as 2 words. Which is why I am using this RegEx;
negWords.replace(/[,.!?;\s]+/g,' ');
That works fine but if people use double space or punctuation it counts that as a word also;
' hello,,' Is counted as 2 words,
but it doesn't count more occurrences as more than 1 word.
' hello,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ' is still counted as 2 words.
Edited for more context.
I have a word counter function but it doesn't account for people using poor punctuation, for example:
"hello.world"
That would only count is as 1 word. Instead it should count that as 2 words. Which is why I am using this RegEx;
negWords.replace(/[,.!?;\s]+/g,' ');
That works fine but if people use double space or punctuation it counts that as a word also;
' hello,,' Is counted as 2 words,
but it doesn't count more occurrences as more than 1 word.
' hello,,,,,,,,,,,,,,,,,,,,,,,,,,,,,, ' is still counted as 2 words.
Edited for more context.
Share Improve this question edited Sep 2, 2013 at 11:13 Philip Richardson asked Sep 2, 2013 at 11:02 Philip RichardsonPhilip Richardson 954 silver badges13 bronze badges 2- 1 I don't get what you want. Can you be clearer ? And please don't speak of count words if you're trying to fix the replace. – Denys Séguret Commented Sep 2, 2013 at 11:05
- See my updated question, hopefully that will make it clearer. – Philip Richardson Commented Sep 2, 2013 at 11:17
2 Answers
Reset to default 5To get the words from your text, you can do
var words = text.split(/\W+/).filter(Boolean);
and the count is words.length
.
Here the filter(Boolean)
call removes empty strings from the array.
The +
symbol means that it should accept one or more of each of the characters in the group. If you want only one, then you need to remove the +
.
negWords.replace(/[,.!?;\s]/g,' ');
If you want to catch one punctuation character or space followed any amount of whitespace, try this:
negWords.replace(/[,.!?;\s]\s*/g,' ');