title: javascript string replace sentence
body: <p>I have this scenario on my chatbox where in I want to bypass the string from a built in application and replace it with specific phrase.</p>
  
  <pre><code>&lt;div id="testdiv"&gt;
  This is an example of a sentence that i want to replace.
  &lt;/div&gt;
  </code></pre>
  
  <p>I want to replace the following:    </p>
  
  <ul>
  <li>"<strong>This is an example of a</strong>" with "<strong>I replaced the</strong>"</li>
  <li>"<strong>that i want to replace</strong>" with "<strong>using javascript</strong>"</li>
  </ul>
  
  <p>The word "sentence" should remain and wont be affected.</p>
  
  <p>So the <strong>output</strong> will be: <code>I replaced the sentence using javascript</code></p>
  
  <p>I used this code of mine, 2 function and it is working but I'm looking for someone's help to clean my code or other method like <strong>split</strong> or <strong>concat</strong> if applicable? And if possible to put it on 1 function with the same idea.</p>
  
  <pre><code>function replace1 () {
        var str1 = document.getElementById("testdiv").innerHTML;
        var ptrn1 = /This is an example of a/gi;
        var rep1 = "This is an example of a";
        var newstr1 = rep1.replace(ptrn1, "that i want to replace");
        document.getElementById("testdiv").innerHTML = newstr1;
    }
  
    function replace2 () {
        var str2 = document.getElementById("testdiv").innerHTML;
        var ptrn2 = /that i want to replace/gi;
        var rep2 = "that i want to replace";
        var newstr2 = rep1.replace(ptrn2, "using javascript");
        document.getElementById("testdiv").innerHTML = newstr2;
    }   
  </code></pre>