Posted By

alvaroisorna on 07/04/07


Tagged

javascript replace String prototype


Versions (?)

Who likes this?

3 people have marked this snippet as a favorite

joseluis
korzhik
hans


String.replaceAll method implementation


 / Published in: JavaScript
 

URL: http://kaedatorum.net/

This snippet adds a replaceAll method to the String prototype, so that you can call it later like this: "test string".replaceAll("s", "-"); // returns "te-t -tring"

  1. String.prototype.replaceAll = function(pcFrom, pcTo){
  2. var i = this.indexOf(pcFrom);
  3. var c = this;
  4.  
  5. while (i > -1){
  6. c = c.replace(pcFrom, pcTo);
  7. i = c.indexOf(pcFrom);
  8. }
  9. return c;
  10. }

Report this snippet  

You need to login to post a comment.