将 HTML 文档正文中的 javascript document.write() 调用替换为其 HTML 内容
PHP 的正则表达式有 AND
运算符吗?
我正在尝试将 document
中的所有内容替换为 '
AND )
。
$output = preg_replace('/document.*\'/', '', $output);
我试图找到一些正则表达式的教程,但我找不到任何好的东西。
编辑:误解。
这是替换之前的代码。
<p>document.write(unescape('
<embed src="XXXXXX" type="application/x-shockwave-flash" wmode="window" width="712" height="475"%.35" allowFullScreen="true" ></embed>
')));</p>
我想让它看起来像这样:
<p>
<embed src="XXXXXX" type="application/x-shockwave-flash" wmode="window" width="712" height="475"%.35" allowFullScreen="true" ></embed>
</p>
替换:
document.write(unescape('
和
')));
Is there an AND
operator for PHP's regular expressions?
I'm trying to replace everything from document
to '
AND )
.
$output = preg_replace('/document.*\'/', '', $output);
I've tried to find some tutorial for regex, but I can't find anything good.
EDIT: Misunderstanding.
This is the code before replaced.
<p>document.write(unescape('
<embed src="XXXXXX" type="application/x-shockwave-flash" wmode="window" width="712" height="475"%.35" allowFullScreen="true" ></embed>
')));</p>
I want to make it look like this:
<p>
<embed src="XXXXXX" type="application/x-shockwave-flash" wmode="window" width="712" height="475"%.35" allowFullScreen="true" ></embed>
</p>
Replaced:
document.write(unescape('
and
')));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
你真正想要的是替换两个部分,并在中间留下一些东西。为了不使其匹配不需要的部分,请使用显式字符类:
因此它会匹配
('
和')
中包含的任何内容以及不同数量的后者。What you actually want is to replace two parts, and leave something in between over. To not make it match undesired parts, use explicit character classes:
So it matches anything enclosed in
('
and')
with varying amounts of the latter.您的意思是
OR
吗?您想要删除范围
["document", "'"]
和范围["document", ")"]
,对吧?这类似于范围["document", "'" OR ")"]
。在正则表达式中,
|
表示“或”。Do you mean
OR
?You want to strip ranges
["document", "'"]
and ranges["document", ")"]
, right? Which is like the range["document", "'" OR ")"]
.In regular expressions,
|
means "or".在正则表达式中,AB 是
A 和 B
A|B 是A OR B
所以如果你想匹配
')
那么只需将其放入正则表达式。另一方面,如果您想匹配
'
或)
作为字符串结尾,请使用'|)
或[' )]
使用类似 http://gskinner.com/RegExr/ 来尝试你的正则表达式。右侧也有说明。
In regular expression, AB is
A and B
A|B isA OR B
So if you want to match
')
then just put that in the regular expression.On the other hand if you want to match either
'
or)
as the end of your string use'|)
or[')]
Use something like http://gskinner.com/RegExr/ to try out your regexes. It also has descriptions on the right.
$output = preg_replace("/document[^']*'([^']*)[^;]*;/", '$1', $输出);
$output = preg_replace("/document[^']*'([^']*)[^;]*;/", '$1', $output);