JSFiddle - React, Tailwind, and code Playground
JavaScript
mnameA = /foo/g;
mnameB = "teach_subfun";
code= "document.writ('this foo must not be replaced'); foo(n);";
result = code.split(/'|"/).map(function (e, i){
return i % 2 ? e : e.replace(mnameA, mnameB);
}).join("");
alert(result);