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?

0

5 Answers 5

3

You could use RegExp with replace

str.replace(/(teerts|vickenbauer)/g, "");
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

1

Sure!

"1000 g teerts smarg 700 vickenbauer 400".replace(/teerts|vickenbauer/g,"");

Comments

1

With regex?

str.replace(/(teerts|vickenbauer)/g, '');

Comments

0
str.replace(new RegExp(/teerts|vickenbauer/g), "");

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.