最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex to replace multiple characters - Stack Overflow

programmeradmin1浏览0评论

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,' ');
发布评论

评论列表(0)

  1. 暂无评论