<script>
function myFunction()
{
var str=document.getElementById("zebra").innerHTML;
var n=str.replace("zebra","horse");
document.getElementById("zebra").innerHTML=n;
}
</script>
however the str zebra might be repeated many times throughout the text. Is there anyway to have it replace all with a click instead of having the user to press the button multiple times ? thank you in advance
however the str zebra might be repeated many times throughout the text. Is there anyway to have it replace all with a click instead of having the user to press the button multiple times ? thank you in advance
Code:
var n=str.replace(/zebra/gi,"horse");
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.
Of course
var n=str.split("zebra").join("horse");
won't work neither will
var n=str.replace(/zebra/gi,"horse");
IF you do not want fazebrain to
become fahorsein.
You need to escape (with a backslash) the following META characters in a regex pettern so thay are interpreted as literals and not control characters:-
^ $ \ / ( ) | ? + * [ ] { } . (but not , )
So:-
Code:
<script type = "text/javascript">
var str = "abc))xyz))";
str = str.replace(/\)/g,"something")
alert (str);
</script>
__________________
All the code given in this post has been tested and is intended to address the question asked.
Unless stated otherwise it is not just a demonstration.