简体   繁体   English

javascript eval()函数中的HTML标记

[英]HTML tag inside javascript eval() function

Why in the first case, html em tags are printed normally whereas in the second test, they disappear. 为什么在第一种情况下,html em标签正常打印,而在第二种测试中,它们消失了。

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. 我看了一下文档https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/eval,但是找不到解释。

Thanks 谢谢

var text = "text";
var textx = text.replace(/(.*)(ex)(.*)/gi,'$1<em>$2</em>$3'); 
console.log(textx) //textx -> text

the problems was that you used a string in the regular expression. 问题是您在正则表达式中使用了字符串。 "/(.*)(ex)(.*)/gi" -> /(.*)(ex)(.*)/gi "/(.*)(ex)(.*)/gi" ( /(.*)(ex)(.*)/gi "/(.*)(ex)(.*)/gi" -> /(.*)(ex)(.*)/gi

and you had a spelling mistake in the console.log(testx) -> console.log(textx) 并且您在console.log(testx) -> console.log(textx)有拼写错误

Because the first uses a regex to match text and the second uses a string. 因为第一个使用正则表达式来匹配文本,第二个使用字符串。

There is no "/(.*)(ex)(.*)/gi" inside "text" . "text"没有"/(.*)(ex)(.*)/gi" "text" There is /(.*)(ex)(.*)/gi . /(.*)(ex)(.*)/gi

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM