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
  • 19,447
  • 18
  • 48
  • 69

5 Answers5

3

You could use RegExp with replace

str.replace(/(teerts|vickenbauer)/g, "");
drinchev
  • 18,618
  • 3
  • 63
  • 91
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
  • 666,277
  • 106
  • 705
  • 986
  • 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,502
  • 4
  • 36
  • 64
1

With regex?

str.replace(/(teerts|vickenbauer)/g, '');
Andreas Wong
  • 57,842
  • 19
  • 104
  • 123
0
str.replace(new RegExp(/teerts|vickenbauer/g), "");
sarwar026
  • 3,773
  • 3
  • 25
  • 36