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;
}

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

Post a Comment

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

Shallow Copy Deep Copy Java example

Database Connection using NODE JS example