Click here to Skip to main content
15,887,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code :

JavaScript
String.prototype.replaceAll = function(search, replace){
    if(!replace){
        return this;
    }
    
    while(this.indexOf(search) !== false){
        this.replace(search, replace);
    }
    
    return this;
};


However, it doesn't works. What can I do?
Posted
Comments
Did you debug? What is the exact issue?

1 solution

This is one that works:
JavaScript
function replaceAll(strString, oldValue, newValue) {
    while (strString.indexOf(oldValue) != -1) {
        strString = strString.replace(oldValue, newValue);
    }
    return strString;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900