I want to replace all occurences of a pattern in a string by another string. For example, lets convert all \"$\" to \"!\":
\"$$$\" -> \"!!!\"
<
There is no standard regexp escape function in Javascript.
You can write your own (source) or get it from your library (dojo example)
function escape(s) {
return s.replace(/[-/\\^$*+?.()|[\]{}]/g, '\\$&')
};
This allows us to create the custom regex object at runtime
function replace_all(string, pattern, replacement){
return string.replace( new RegExp(escape(pattern), "g"), replacement);
}