JavaScript USES the replace function to replace a string

  • 2020-05-26 07:48:36
  • OfStack

This example shows how JavaScript replaces strings with the replace function. Share with you for your reference. The details are as follows:

JavaScript replaces the string with the replace function, and the following code replaces MicroSoft in Visit Microsoft with ofstack.com


<!DOCTYPE html>
<html>
<body>
<p>
Click the button to replace "Microsoft" 
with "ofstack.com" 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 n=str.replace("Microsoft","ofstack.com");
document.getElementById("demo").innerHTML=n;
}
</script>
</body>
</html>

I hope this article is helpful for you to design javascript program.


Related articles: