We Recommend

Pro JavaScript Techniques Pro JavaScript Techniques
Pro JavaScript Techniques is the ultimate JavaScript book for the modern web developer. It provides everything you need to know about modern JavaScript, and shows what JavaScript can do for your web sites. This book doesn't waste any time looking at things you already know, like basic syntax and structures.


Posted By

alvaroisorna on 07/04/07


Tagged

javascript replace String prototype


Versions (?)


Who likes this?

2 people have marked this snippet as a favorite

joseluis
korzhik


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.