Why in the first case, html em tags are printed normally whereas in the second test, they disappear.
var text = "text";
eval("var text = text.replace(/(.*)(ex)(.*)/gi,'$1<em>$2</em>$3');");
console.log(text) //text -> t<em>ex</em>t
but
var textx = text.replace("/(.*)(ex)(.*)/gi",'$1<em>$2</em>$3');
console.log(textx) //textx -> text
I’ve looked at the documentation
https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/eval but can’t find an explanation.
Thanks
Because the first uses a regex to match text and the second uses a string.
There is no
"/(.*)(ex)(.*)/gi"inside"text". There is/(.*)(ex)(.*)/gi.