JavaScript replace all

This is a little example how to replace all matches with replace.
<!DOCTYPE html>
<html>
<body>

<p>Click the button to replace "i" with "X" in the paragraph below:</p>

<p id="demo">Visit Microsoft!</p>

<button onclick="myFunction()">Try it</button>

<script>
function myFunction() {
    var str = document.getElementById("demo").innerHTML; 
    var res = AllReplace(str,"i", "X");
    document.getElementById("demo").innerHTML = res;
}

function AllReplace(str,find,replace){
while(str != str.replace(find,replace)){
  str = str.replace(find,replace);
}
return(str);
}

</script>

</body>
</html>

Click the button to replace "i" with "X" in the paragraph below:

Visit Microsoft!

 

One thought on “JavaScript replace all”

  1. You’re so awesome! I don’t believe I’ve read through a single thing like that before.
    So good to discover another person with unique thoughts on this
    topic. Seriously.. thank you for starting this up.
    This website is something that’s needed on the internet, someone
    with a little originality!

Leave a Reply

Your email address will not be published.

twenty + 11 =