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


Twitter Facebook Google LinkedIn Email Pinterest Reddit StumbleUpon Tumblr



Try these Fun Games by Bobbie:


Travel Blast Game New York


Play and Learn Russian


Battlestarship Game



Take a look at these Groovy Codes:


Fun IQ Test


Html Svg Starburst


Javascript Particles Fishes



Read the Latest Breaking Programming and Tech News, Great Articles and Tips:


Codenewz Programming and Tech News







Comments

Popular posts from this blog

Multi-tap Keypad Text Entry

Crypto Mining