Time remaining:
replacing multiple instances in javascript string

Computer Science
Tutor: None Selected Time limit: 0 Hours

what is the best way to replace multiple instances of the same string in javascript?

Mar 20th, 2015

<!DOCTYPE html>

<html>

<body>

<button onclick="replaceText()">Replace</button>

<p id="textbox"> The best way to replace multiple instances of the same string in javascript.<br>

The best way to replace multiple instances of the same string in javascript.<br>

The best way to replace multiple instances of the same string in javascript.

</p>

<script>

function replaceText(){            

    var s1 = document.getElementById("textbox").innerHTML;

    var s2 = s1.replace(/way/gi,"method");

     document.getElementById("textbox").innerHTML = s2;

}

</script>

</body>

</html>

Hi...try this code.... this javascript + html code replacing the multiple instances of same string. In this example,  " way"  string is replaced by "method". In this "gi"  regexp is used for global case-insensitive search.

Mar 21st, 2015

Studypool's Notebank makes it easy to buy and sell old notes, study guides, reviews, etc.
Click to visit
The Notebank
...
Mar 20th, 2015
...
Mar 20th, 2015
Dec 4th, 2016
check_circle
Mark as Final Answer
check_circle
Unmark as Final Answer
check_circle
Final Answer

Secure Information

Content will be erased after question is completed.

check_circle
Final Answer