正则表达式:查找并删除包含在另一个标签中的 HTML 标签

正则表达式:查找并删除包含在另一个标签中的 HTML 标签

我有 2 个段落,带有 html 标签<p class="mb-40px"></p>此标签内有文本和另一个 <p..> 标签

我需要查找并删除主标签中包含的 html 标签<p class="mb-40px"></p>

例子:

<p class="mb-40px">Miracles every day <p lang="ro-RO" align="JUSTIFY">being at la home, the poet Petru Iliesu made an international appeal to condemn the killing of demonstrators, shot on the streets of town.</p>

<p class="mb-40px">The pleasure we feel after reading a book is different from the pleasure of tasting a dish <p class="dis23">. In the first case, pleasure is disinterested, having no particular purpose, as a release from external preoccupations.</p>

输出

<p class="mb-40px">Miracles every day being at la home, the poet Petru Iliesu made an international appeal to condemn the killing of demonstrators, shot on the streets of town.</p>

<p class="mb-40px">The pleasure we feel after reading a book is different from the pleasure of tasting a dish. In the first case, pleasure is disinterested, having no particular purpose, as a release from external preoccupations.</p>

我的正则表达式不太好:

寻找: (?:<p class="mb-40px">|\G)\S*\K(?:\h+<\s*(\w+)[^/>]*>)(?=.*</p>)

替换为:(留空)

答案1

这是您修改后的公式,对我而言有效:

<p class="mb-40px">.*\K(?:\h+<\s*(\w+)[^\/>]*>)(?=.*<\/p>)

  • 你不需要使用?:<\G如果你无论如何使用\K
  • \S*.*仅当标签之间只有一个单词且没有空格时才有效,请使用
  • /需要转义:\/

在此处输入图片描述

下面是一个更短的表达式:

<p class="mb-40px">.*\K<p[^>]+>

答案2

是的,我设法自己找到解决方案:

寻找:(?-i:<p class="mb-40px">|\G(?!^))(?s:(?!</p>).)*?\K(?-i:<\s*(\w+)[^/>]*>)

替换为:LEAVE EMPTY

因此,如果我想查找/替换其他两个部分中的内容,通用正则表达式将是:

(?-i:REGION-START|\G(?!^))(?s:(REGION-FINAL).)*?\K(?-i:FIND REGEX)

相关内容