Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple replaces with javascript [duplicate]

In PHP, you do this to replace more then one value at a time.

<?php $string = "i am the foobar";  $newstring = str_replace(array('i', 'the'), array('you', 'a'), $string);  echo $newstring; ?> 

How do you do this in javascript?

like image 976
Marwelln Avatar asked Jul 20 '10 19:07

Marwelln


1 Answers

Use javascript's .replace() method, stringing multiple replace's together. ie:

var somestring = "foo is an awesome foo bar foo foo"; //somestring lowercase var replaced = somestring.replace(/foo/g, "bar").replace(/is/g, "or"); // replaced now contains: "bar or an awesome bar bar bar bar" 
like image 129
Alex Avatar answered Sep 28 '22 03:09

Alex