I want to replace all the occurrences of a string in javascript;
In my search
text I have special chars and I cant escape them;
I need to replace the ${wid}
text with the given string;
I have this function:
function replaceAll(find, replace, str) {
return str.replace(new RegExp(find, 'g'), replace);
}
that should do the job;
var inner_code = replaceAll('/\$\{wid\}/', 'id_' + widget.model_attributes.id, widget.code);
widget.model_attributes.id
is a positive integer number
widget.code
is:
<div id="code_50"><div id="${wid}"></div>
<script type="text/javascript">
$("#${wid}").text("hello world");
</script></div>
p.s: this is not a duplicate question; i don't know how to escape special chars using JS