How To Use String Object Replace In JavaScript

In this article I am going to explain about String Object Replace In JavaScript.
  • 1873

Use replace() method in JavaScript

The replace() method searches a string for a specified value.

This method returns a new string where the specified values are replaced.

All browsers support string object replace() method.

Syntax

string.replace(searchvalue,newvalue)

Example

<!DOCTYPE html>

<html>

<body style ="background-color :Red">

 

<p>Click the button to replace "Microsoft" with "dotnetheaven" in the paragraph below:</p>

 

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

 

<button onclick="myFunction()">Click here</button>

 

<script type="text/javascript">

    function myFunction()

 {

        var str = document.getElementById("demo").innerHTML;

        var n = str.replace("Microsoft", "dotnetheaven");

        document.getElementById("demo").innerHTML = n;

    }

</script>

 

</body>

</html>

Output1

 op1.jpg

Output2

 op2.jpg

Further Readings

You may also want to read these related articles :here

Ask Your Question 

Got a programming related question? You may want to post your question here

Programming Answers here

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.