从HTML中删除所有http和https,但不包括占位符

问题描述 投票:2回答:1

我想删除HTML文件中的所有http:https:,但不包括placeholder="http:placeholder="https:。我尝试了以下示例,但每个http:和https:都将被删除:

/(?!placeholder=")(http:|https:)/
php regex preg-replace
1个回答
4
投票

您需要用lookbehind替换前瞻。此外,您可以将交替减少到仅仅https?:模式,其中s?表示1或0 s

'/(?<!placeholder=")https?:/'
    ^                   ^^

如果要确保placeholder匹配为整个单词,请添加单词边界:

'/(?<!\bplaceholder=")https?:/'
      ^^

如果在placeholder之前必须有空格,请将\b替换为\s

细节

  • (?<!\bplaceholder=") - 一个字符串内的一个位置,紧接着一个完整的单词placeholder然后="
  • http - 一个http子串
  • s? - 可选的s
  • : - 一个冒号。
© www.soinside.com 2019 - 2024. All rights reserved.