javascript - Regex to replace multiple characters - Stack Overflow

I have a word counter function but it doesn't account for people using poor punctuation, for examp

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
Add a ment  | 

2 Answers 2

Reset to default 5

To 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,' ');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745321497a4622463.html

相关推荐

  • javascript - Regex to replace multiple characters - Stack Overflow

    I have a word counter function but it doesn't account for people using poor punctuation, for examp

    11小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信