In PHP, you do this to replace more then one value at a time.
<?php
$string = "jak har en mamma";
$newstring = str_replace(array('jak', 'mamma'), array('du', 'pappa'), $string);
echo $newstring;
?>
How do you do this in javascript?
In PHP, you do this to replace more then one value at a time.
<?php
$string = "jak har en mamma";
$newstring = str_replace(array('jak', 'mamma'), array('du', 'pappa'), $string);
echo $newstring;
?>
How do you do this in javascript?
Use javascript's .replace()
method, stringing multiple replace's together. ie:
var someString = "foo is an awesome foo bar foo foo";
var replaced = somestring.replace(/foo/g, "bar").replace(/is/g, "or");
// replaced now contains: "bar or an awesome bar bar bar bar"
You could do:
var string = "jak har en mamma";
string = string.replace(/(jak)|(mamma)/g,function(str,p1,p2) {
if(p1) return 'du';
if(p2) return 'pappa';
});
or:
var string = "jak har en mamma";
string = string.replace(/jak/g,'du').replace(/mamma/g,'pappa');
Still didnt work to replace spaces.
I did this and it worked:
var $val = $(this).val().replace(",", ".").split(' ').join('');
Thanks anyway!