Here is the latest function posted as code:
String.prototype.replaceAll = function(str1, str2, ignore)
{
return this.replace(new RegExp(str1.replace(/([\/\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g,"\\$&"),(ignore?"gi":"g")),(typeof(str2)=="string")?str2.replace(/\$/g,"$$$$"):str2);
}
Here is a modified version that corrects the issues you raise and also supports "ignore case" as an optional boolean.
You just need to escape all the regex special characters.
String.prototype.replaceAll = function(str1, str2, ignore)
{
return this.replace(new RegExp(str1.replace(/([\,\!\\\^\$\{\}\[\]\(\)\.\*\+\?\|\<\>\-\&])/g, function(c){return "\\" + c;}), "g"+(ignore?"i":"")), str2);
};
This code adds a replaceAll method to the String object.
You call it like this:
var myString = "Hello???";
myString.replaceAll("?", "!");
And it will return this:
Hello!!!
See if you can break it.