Javascript .replace() / 正则表达式,代码为字符串
我正在尝试“删除”(只需将其替换为“”)以下字符串:
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
/codeString/
是搜索文字“codeString”的正则表达式,而不是包含模式的变量。此外,string.replace(pattern, replacement)
不会修改string
,而是返回应用了替换的新字符串。您不需要像这样的正则表达式来删除固定字符串,下一个代码将从
code
中删除一次字符串:/codeString/
is a regular expression searching for the literal "codeString", not a variable containing a pattern. Also,string.replace(pattern, replacement)
does not modifystring
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
:好吧,你可以从
如果它需要是正则表达式,你可以使用:
但要注意正则表达式元字符应该用反斜杠转义。
Well you can start with
If it needs to be a regular expression, you can use:
but be aware that the regex metacharacters should be escaped with backslashes.