Javascript .replace() / 正则表达式,代码为字符串

发布于 2024-12-04 11:56:17 字数 1468 浏览 0 评论 0原文

我正在尝试“删除”(只需将其替换为“”)以下字符串:

<script type="text/javascript">
document.createElement("img").src="http://www.google.com"
</script>

从以下字符串:

<SCRIPT LANGUAGE=JavaScript>
var PAswf = "_ADPATH_[%SWF File%]";
var advurl = "_ADCLICK_";
var PAadvurl = escape(advurl);
var PAwidth = "[%Width%]";
var PAheight = "[%Height%]";
var wmValue = "_ADDCP(wm)_";
if (!wmValue)wmValue = "opaque";
var PAwmode = wmValue;
document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
document.write(' WIDTH="'+ PAwidth +'" HEIGHT="'+ PAheight +'">');
document.write('<PARAM NAME=movie VAL'+'UE="' + PAswf + '?clickTag='+ PAadvurl +'"><PARAM NAME=quality VALUE=high><PARAM NAME=wmode VALUE='+ PAwmode +'>');
document.write('<EMBED sr'+'c="'+PAswf + '?clickTag='+ PAadvurl +'" quality=high wmode='+ PAwmode);
document.write(' swLiveConnect=TRUE WIDTH="'+ PAwidth +'" HEIGHT="'+ PAheight +'"');
document.write(' TYPE="application/x-shockwave-flash">');
document.write('</EMBED></OBJECT>');
</SCRIPT>
<script type="text/javascript">
document.createElement("img").src="http://www.google.com"
</script>

如您所见,该字符串位于代码块的底部,但这并不重要。我已将第一个字符串存储为变量 codeString,并尝试了以下操作。

// code refers to the bigger code string
code.replace(/codeString/gm, "");

好像不太匹配啊我确信我做错了什么,但我搜索了大约一个小时却一无所获。有什么想法吗?

I'm trying to "remove" (just replace it with "") the following String:

<script type="text/javascript">
document.createElement("img").src="http://www.google.com"
</script>

From the following String:

<SCRIPT LANGUAGE=JavaScript>
var PAswf = "_ADPATH_[%SWF File%]";
var advurl = "_ADCLICK_";
var PAadvurl = escape(advurl);
var PAwidth = "[%Width%]";
var PAheight = "[%Height%]";
var wmValue = "_ADDCP(wm)_";
if (!wmValue)wmValue = "opaque";
var PAwmode = wmValue;
document.write('<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"');
document.write(' WIDTH="'+ PAwidth +'" HEIGHT="'+ PAheight +'">');
document.write('<PARAM NAME=movie VAL'+'UE="' + PAswf + '?clickTag='+ PAadvurl +'"><PARAM NAME=quality VALUE=high><PARAM NAME=wmode VALUE='+ PAwmode +'>');
document.write('<EMBED sr'+'c="'+PAswf + '?clickTag='+ PAadvurl +'" quality=high wmode='+ PAwmode);
document.write(' swLiveConnect=TRUE WIDTH="'+ PAwidth +'" HEIGHT="'+ PAheight +'"');
document.write(' TYPE="application/x-shockwave-flash">');
document.write('</EMBED></OBJECT>');
</SCRIPT>
<script type="text/javascript">
document.createElement("img").src="http://www.google.com"
</script>

As you can see, the String is at the bottom of the code block, but that shouldn't really matter. I've stored the first string as a variable, codeString, and tried the following.

// code refers to the bigger code string
code.replace(/codeString/gm, "");

Doesn't seem to match. I'm sure I'm doing something wildly wrong, but I've searched for about an hour and have come up empty. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

瘫痪情歌 2024-12-11 11:56:17

/codeString/ 是搜索文字“codeString”的正则表达式,而不是包含模式的变量。此外,string.replace(pattern, replacement) 不会修改string,而是返回应用了替换的新字符串。

您不需要像这样的正则表达式来删除固定字符串,下一个代码将从 code 中删除一次字符串:

var codeString = '<script type="text/javascript">\n' +
    'document.createElement("img").src="http://www.google.com"\n' +
    '<\/script>';
code = code.replace(codeString, "");

/codeString/ is a regular expression searching for the literal "codeString", not a variable containing a pattern. Also, string.replace(pattern, replacement) does not modify string but returns a new string with the replacement applied.

You do not need a regular expression for stripping fixed strings like this, the next code will remove the string once from code:

var codeString = '<script type="text/javascript">\n' +
    'document.createElement("img").src="http://www.google.com"\n' +
    '<\/script>';
code = code.replace(codeString, "");
安穩 2024-12-11 11:56:17

好吧,你可以从

code.replace(codestring, "");

如果它需要是正则表达式,你可以使用:

code.replace(new RegExp(codestring, "gm"), "");

但要注意正则表达式元字符应该用反斜杠转义。

Well you can start with

code.replace(codestring, "");

If it needs to be a regular expression, you can use:

code.replace(new RegExp(codestring, "gm"), "");

but be aware that the regex metacharacters should be escaped with backslashes.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文