正则表达式捕捉 <strong >strong</strong >

Regex to catch <strong >strong</strong >

我需要一个正则表达式来捕获文本周围的 < strong >< /strong >< strong > text < /strong >,但是我想避免删除 [=14] 中间的文本=] 和 < /strong >

基本上在

之后
preg_replace($expression, "", "<strong>STRONG</strong>"); 

只会给我 STRONG 没有 < strong >< /strong >

我目前有:

<\w*>\w*<\/\w*>

但是;这也捕获了标签之间的文本。

您可以在捕获组中捕获您不想删除的文本:

<\w*>(\w*)</\w*>

并替换为 </code> 以避免删除捕获的文本。</p> <p>或者,您可以使用模式</p> <pre><code></?\w*>

只匹配开始和结束标签,不匹配中间的文本。但是,这也会删除没有相应 opening/closing 标签的杂散标签。

如果您只想去除 strong 的开始和结束标签,那么这就足够了:

$out = preg_replace(array('$\<\s?strong\s?\>$si', '$\</\s?strong\s?\>$si'), '', "<strong>STRONG</strong>");

您可以使用这个正则表达式:

<\/?strong>

命令:

preg_replace("/<\/?strong>/", "", "<strong>Hello</strong>");

Online demo