Feb 23 2009
In PHP, str_replace will replace all the occurrence key in a string to the value that specified by user.
In Javascript, if you using a replace function without specified, it will only replace the first occurrence of the key in the string to the value.
For example :
<script>
var js_str = "Visit RedHat now & RedHat will give out free gift to you";
js_str.replace("RedHat","how2guru");
alert(js_str);
</script>
The output of the above script will give you :
Visit how2guru now & RedHat will give out free gift to you
If you would like to replace all the occurrence key to the value, you have to use the global match. It will replace all the occurrence key.
read more »
3 Comments | Tags: Javascript, replace all














