Javascript Data Type How to - Replace certain characters








Question

We would like to know how to replace certain characters.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
<!--   w  w w  . ja v a 2 s .  c  o m-->
String.prototype.replaceAll = function(v, s){
    var i , ret = this;
    for(i=0;i<v.length;i++)
        ret = ret.replace(v[i], s[i]);
    return ret;
}
document.writeln("a b c d e f".replaceAll(["a", "b"], ["1", "2"]));

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: