Prev Demo
JavaScript
>
Replace() Method
Next Demo
Write code here
J
|
B
|
A
<!DOCTYPE html> <html> <head> <title>Demo</title> </head> <body> <h1>Replace the elements in a string with new elements.</h1> <p>Click the below button to search for a Match.</p> <input type="button" value="Replace" onclick="Function()" /> <p id="myId">This is DotNetFunda</p> <script> function Function() { var A = "This is DotNetFunda DotNetFunda"; // replace 1st occurance var r = A.replace("DotNetFunda", "TechFunda"); document.getElementById("myId").innerHTML = r; // replace all occurance r = A.replace(/DotNetFunda/g, "TechFunda"); document.getElementById("myId").innerHTML += "<br />" + r; } </script> </body> </html>
Note: We DO NOT save your trial code in our database.
Output