Wednesday, November 6, 2013

ReplaceAll Function in Javascript

Replace All Function in Javascript : Implemented through Recursion
 function replaceAll(word, text, newWord){
  var pos = text.search(word);
  if(pos == -1)
   return text;
  var str1 = text.substr(0, pos);
  var str2 = replaceAll(word, text.substr(pos+word.length), newWord);
  return str1+newWord+str2;
 }

No comments: