我想删除其他 2 个标签内的所有 html 标签,除了<a href=.*?">
和</a>
例如:
<p class="mb-40px">Another blending </h2>option is to all the <div>brushstrokes to show. In the painting of trees above, I didn’t spend much time trying to <a href=https://orfun.com/acrylic class="color-bebe" target="_new">blend the colors</a>. I simply mix each color and apply it without fussing with it.</p>
输出:
<p class="mb-40px">Another blending option is to all the brushstrokes to show. In the painting of trees above, I didn’t spend much time trying to <a href=https://orfun.com/acrylic class="color-bebe" target="_new">blend the colors</a>. I simply mix each color and apply it without fussing with it.</p>
我的正则表达式不是很好:
寻找:<p class="mb-40px">.*?</p>(?!</a>)|(?!<a href=.*?">)
答案1
- Ctrl+H
- 找什么:
(?:<p class="mb-40px">|\G).*?\K(?:<a href=.+?</a>(*SKIP)(*FAIL)|<(?:(?!/?p).)+?>)
- 用。。。来代替:
LEAVE EMPTY
- 打钩 环绕
- 选择 正则表达式
- Replace all
解释:
(?: # non capture group
<p class="mb-40px"> # literally
| # OR
\G # restart from last match position
) # end group
.*? # 0 or more any character
\K # forget all we have seen until this position
(?: # non capture group
<a href=.+?</a> # <a href=...
(*SKIP)(*FAIL) # skip this match and consider it has failled
| # OR
<(?:(?!/?p).)+?>) # any tag that is not </p>
) # end group
截图(之前):
截图(之后):
答案2
寻找: (?:<p class="mb-40px">|\G).*?\K(?:<a href=.*?>.*?</a>(*SKIP)(*FAIL)|<(?!/?p|/?a)[^>]*>)
替换为: LEAVE EMPTY
或者
寻找: (?:<p class="mb-40px">|\G).*?\K(?:<a href=.+?</a>(*SKIP)(*FAIL)|<(?:(?!/?p class="mb-40px">|/p>).)+?>)
替换为: LEAVE EMPTY
或者:
寻找: <p class="mb-40px">(?:[^<]+|<(?!/?[ap]|a\s[^>]>))?</p>(*SKIP)(FAIL)|<(?!/?[ap]|a\s[^>]>)[^>]+>
替换为: LEAVE EMPTY
元素 [ap] 是与“a”或“p”匹配的字符类。在正则表达式中,使用字符类可以指定模板中给定位置的一组可能字符。
在我们的例子中,[ap] 用于 <(?!/?[ap]|a\s[^>]*>) 部分,我们在此检查以“<”开头且后面不跟“a”或“p”的标签。通过使用 [ap],我们表示我们希望“<”后的下一个字母是“a”或“p”。因此,任何不以“a”或“p”开头的标签都将包含在正则表达式匹配中并被替换。
我希望这个解释能够阐明所提议的正则表达式中 [ap] 元素的用法。