Javascript Code to Replace a Word in a String
This code will Replace a Word in a String using Javascript
Javascript
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Replace a Word in a String</title>
</head>
<body>
<p id="myText">My favorite color is red</p>
<button type="button" onclick="strReplace();">Replace</button>
</body>
<script>
function strReplace(){
var myStr = 'My favorite color is red';
var newStr = myStr.replace(/red/g, "purple");
document.getElementById("myText").innerHTML = newStr;}
</script>
</html>
Click here to run this code
Share with Friends









Comments
Post a Comment