title: Replace all using indexOf and while loop in String.prototype
body: <p>I am trying to implement in my code this string replacement function:</p>
  
  <pre><code>String.prototype.replaceAll = function(f,r) {
    if (f != r) {
      while (this.indexOf(f) !== -1) {
        this = this.replace(f,r);
      }
    } else {
      return this;
    }
  };
  </code></pre>
  
  <p>And I already tried to use this:</p>
  
  <pre><code>String.prototype.replaceAll = function(f,r) {
    return this.split(f).join(r);
  };
  </code></pre>
  
  <p>But this last replacement function <strong>does not work</strong> with two or more characters in the search expression. </p>
  
  <p>So I really need to use the first function with the while-loop.</p>
  
  <p><strong>Does anybody knows what is the problem in the first function showed?</strong></p>