2

I would like to exclude certain words using a regular expression.

Input Text:

aaa1234 cc bbb1234 c1234 cc dd aacc cccc ccadf cc

Output Text:

aaa1234 bbb1234 c1234 dd aacc cccc ccadf

exclude word: cc

I used (^|\s)[^(cc)]+(\s|$)

How to make the regular expression work?

Jerry
  • 70,495
  • 13
  • 100
  • 144
BlueSi
  • 23
  • 2

2 Answers2

1
\s+\bcc\b|\bcc\b\s+

Try this.Replace by empty string.See demo.

https://regex101.com/r/cK4iV0/25

vks
  • 67,027
  • 10
  • 91
  • 124
0

You can use something like this (^|\s+)cc(\s+|$) to filter word cc, then replace it by empty string

var word='aaa1234 cc bbb1234 c1234 cc dd aacc cccc ccadf cc';

console.log(word.replace(/(^|\s+)cc(\s+|$)/g,' ').trim());
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • Hmm ok, I see that now. It doesn't give the required output `aaa1234 bbb1234 c1234 dd aacc cccc ccadf` though but gives `aaa1234bbb1234 c1234dd aacc cccc ccadf` – Jerry Jul 08 '15 at 05:49
  • The first leaves double spaces, the second leaves a trailing space (at the end). – Jerry Jul 08 '15 at 05:58