Infolinks In Text Ads

Sep 10, 2010

replaceall in javascript example

ReplaceAll method in java script.

function ReplaceAll(content,findWith,replaceWith){
  var str = content;
    var index = str.indexOf(findWith);
        while(index != -1){
            str = str.replace(findWith,replaceWith);
            index = str.indexOf(findWith);
        }
        return str;
}

3 comments:

  1. alert(ReplaceAll("shit","s","s"));

    ReplyDelete
  2. CORRECT:
    function ReplaceAll(content,findWith,replaceWith){
    var str="";
    if ( findWith != replaceWith ) {
    var flen=findWith.length;
    var index=content.indexOf(findWith);
    while ( index != -1 ) {
    str+=content.substr(0,index)+replaceWith;
    content=content.substr(index+flen);
    index = content.indexOf(findWith);
    }
    }
    return str;
    }
    alert(ReplaceAll("fefe","e","ee"));

    ReplyDelete
  3. Thanks for the helps.... is very good.

    ReplyDelete

LinkWithin

Related Posts Plugin for WordPress, Blogger...