How to use Replace in JavaScript

In this article I am going to explain about Replace() method in JavaScript.
  • 2016

JavaScript Replace() Method

JavaScript Replace() method work as a regular expression. Replace() method search with specified value and replace it with a new given values.

Syntax

string.replace(oldvalue, newvalue)

Example

In this example new value "Welcome" will replace to "Hello"

<html>

<head>

    <script type="text/javascript">

        function Myfun() {

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

            var st = str.replace("Hello", "Welcome");

            document.getElementById("rep").innerHTML = st;

        }

</script>

</head>

<body>

    <p>

        Click button for replace value</p>

    <h5 id="rep">

        Hello</h5>

    <button onclick="Myfun()">

        click</button>

</body>

</html>

Output

replace.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
 

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.