【发布时间】:2022-01-25 22:00:06
【问题描述】:
我有一个有效的正则表达式。但是我希望它删除具有特定单词的匹配项。
/\<meta[^\>]+(http\-equiv[^\>]+?refresh[^\>]+?(?<!\-)(?<!\d)[0-9]\d*[^\>]+?url[^\>]+?|(?<!\-)(?<!\d)[0-9]\d*[^\>]+?url[^\>]+?http\-equiv[^\>]+?refresh[^\>]+?)\/?\>/is
这匹配以下内容:(http-equiv 和 url 任意顺序)
<meta http-equiv="refresh" content="21;URL='http://example.com/'" /><meta content="21;URL='http://example.com/'" http-equiv="refresh" />
我想排除任何包含?PageSpeed=noscript的网址
一个。 <meta content="21;URL='http://example.com/?PageSpeed=noscript'" http-equiv="refresh" />
湾。 <meta content="21;URL='http://example.com/segment?PageSpeed=noscript&var=value'" http-equiv="refresh" />
非常感谢任何想法。谢谢。
【问题讨论】:
-
是标准的元标记还是就像你的例子中的分号格式一样?
-
重定向页面的标准元标记。本质上,正则表达式检测页面是否重定向到某个地方。所以“内容”的价值必须是非负的。最后,URL 不能包含
?PageSpeed=noscript -
对于这种特殊情况,我只使用
str_contains,因为它更容易发现和评论异常。可能不是您正在寻找的答案,我理解。 -
You could use a negative lookahead (demo)。如前所述,使用解析器可能是一个更好的主意。
-
@ShivanandSharma 当然有可能。查看更新的答案。