0

Possible Duplicate:
How to replace several words in javascript

I have a string:

var str = "1000 g teerts smarg 700 vickenbauer 400";

I need to replace teerts, vickenbauer by white spaces.

I can do like:

str.replace("teerts", "");
str.replace("vickenbauer", "");

But, is there any way to bind the two into just one line?

Community
  • 1
  • 1
Luca
  • 20,399
  • 18
  • 49
  • 70

5 Answers5

3

You could use RegExp with replace

str.replace(/(teerts|vickenbauer)/g, "");
drinchev
  • 19,201
  • 4
  • 67
  • 93
3

You can chain the replaces:

str = str.replace("teerts","").replace("vickenbauer","");

Note that the replace method doesn't change the string that you use it on, you have to take care of the return value.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Why the downvote? If you don't explain what you think is wrong, it can't improve the answer. – Guffa Apr 22 '12 at 18:31
1

Sure!

"1000 g teerts smarg 700 vickenbauer 400".replace(/teerts|vickenbauer/g,"");
noob
  • 8,982
  • 4
  • 37
  • 65
1

With regex?

str.replace(/(teerts|vickenbauer)/g, '');
Andreas Wong
  • 59,630
  • 19
  • 106
  • 123
0
str.replace(new RegExp(/teerts|vickenbauer/g), "");
sarwar026
  • 3,821
  • 3
  • 26
  • 37