我正在尝试删除CKEditor插入描述框中的所有空<p>
标记,但它们似乎都有所不同。可能性似乎是:
<p></p>
<p>(WHITESPACE)</p>
<p> </p>
<p><br /></p>
<p>(NEWLINE) </p>
<p>(NEWLINE)<br /><br />(NEWLINE) </p>
有了这些可能性,段落之间可能会有任意数量的空格
和<br />
标记,并且在一个段落中可能会有一些类型。
我也不确定<br />
标记,我看到它可能是<br />
,<br/>
或<br>
。
我搜索了类似的答案,但是我看到的所有答案似乎都只适用于其中一种情况,而不是一次性完成。我想简单来说我要问的是,是否有一个正则表达式可用于删除某些HTML中没有任何字母数字文本或符号/标点符号的所有<p>
标记?
答案 0 :(得分:17)
嗯,与我的建议不要用正则表达式解析HTML相矛盾,我写了一个正则表达式来做到这一点:
"#<p>(\s| |</?\s?br\s?/?>)*</?p>#"
这将正确匹配:
<p></p>
<p> </p> <!-- ([space]) -->
<p> </p> <!-- (That's a [tab] character in there -->
<p> </p>
<p><br /></p>
<p>
</p>
<p>
<br /><br />
</p>
它的作用:
# / --> Regex start
# <p> --> match the opening <p> tag
# ( --> group open.
# \s --> match any whitespace character (newline, space, tab)
# | --> or
# --> match
# | --> or
# </?\s?br\s?/?> --> match the <br> tag
# )* --> group close, match any number of any of the elements in the group
# </?p> --> match the closing </p> tag ("/" optional)
# / --> regex end.
答案 1 :(得分:2)
选择的答案很棒,但如果<p>
标记定义了内联样式属性,例如<p style="font-weight:bold">
,则无效。
匹配此的正则表达式将是:
#<p[^>]*>(\s| |</?\s?br\s?/?>)*</?p>#