Programming
replacing multiple instances in javascript string

Question Description

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

Final Answer

<!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.

AnkitP (446)
Boston College

Anonymous
The tutor was pretty knowledgeable, efficient and polite. Great service!

Anonymous
Heard about Studypool for a while and finally tried it. Glad I did caus this was really helpful.

Anonymous
Just what I needed… fantastic!

Studypool
4.7
Trustpilot
4.5
Sitejabber
4.4
Similar Questions
Related Tags

Brown University





1271 Tutors

California Institute of Technology




2131 Tutors

Carnegie Mellon University




982 Tutors

Columbia University





1256 Tutors

Dartmouth University





2113 Tutors

Emory University





2279 Tutors

Harvard University





599 Tutors

Massachusetts Institute of Technology



2319 Tutors

New York University





1645 Tutors

Notre Dam University





1911 Tutors

Oklahoma University





2122 Tutors

Pennsylvania State University





932 Tutors

Princeton University





1211 Tutors

Stanford University





983 Tutors

University of California





1282 Tutors

Oxford University





123 Tutors

Yale University





2325 Tutors