-1

I want to replace every occurence of a question mark, point, comma etc. in a string called TextToSimplify but I keep getting the error as said in the title. What can I do about it

ToSimplifyText = ToSimplifyText.replace(/,/g, "");
ToSimplifyText = ToSimplifyText.replace(/./g, "");
ToSimplifyText = ToSimplifyText.replace(/!/g, "");
ToSimplifyText = ToSimplifyText.replace(/?/g, "");
ToSimplifyText = ToSimplifyText.replace(/'/g, "");
DaddyMike
  • 161
  • 11
  • Use one regex with a character class: `ToSimplifyText = ToSimplifyText.replace(/[,.!?']/g, "");` – p.s.w.g Apr 15 '19 at 16:04

1 Answers1

0

You can do all that stuff on one single replace, like this:

ToSimplifyText = "Hello. Dude! Aren't you ok?"
console.log(ToSimplifyText.replace(/[,.'?!]/g, ""));

Note, that if you want to replace a special character, you will have to escape it with \. Note also, this is not necessary when using a set of characters with [...], like in the previous example:

ToSimplifyText = "Hello. Dude! Aren't you ok?"
console.log(ToSimplifyText.replace(/\?/g, ""));
Shidersz
  • 16,846
  • 2
  • 23
  • 48