Javascript Code to Replace a Word in a String

This code will Replace a Word in a String using Javascript


<!DOCTYPE html>
<title>JavaScript Replace a Word in a String</title>

<p id="myText">My favorite color is red</p>

<button type="button" onclick="strReplace();">Replace</button>



function strReplace(){

var myStr = 'My favorite color is red';

var newStr = myStr.replace(/red/g, "purple");

document.getElementById("myText").innerHTML = newStr;}



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


Popular posts from this blog

Multi-tap Keypad Text Entry

HTML5 Crossword 1