我需要一些帮助来制作一个删除两个 HTML 标记标签之间的 /> 的正则表达式。
<!-- The line could look like this -->
<td align=right valign=bottom nowrap><div>January 24, 2013 /></div></td>
<!-- Or this -->
<div>Is this system supported? /></div>
<!-- Even this -->
<span>This is a span tag /></div>
<!-- It could look like any of these but I do not want /> removed -->
<img src="example.com/example.jpg"/></img>
<img src="example.com/example.jpg"/>
<img src="example.com/example.jpg"/></img>
<div id="example"><img src="example.com/example.jpg"/></div>
(是的,我意识到 img 标签没有与之关联的结束标签。我正在动态编辑我尚未创建的无数页面;这不是我的标记。)
这是我想出的正则表达式(使用 perl):
s|(<.*?>(?!<img).*?)(\s*/>)(?!</img>)(</.*?>)|$1$3|gi;
是否有更好的正则表达式更有效或更快?
将正则表达式应用于上述示例后,结果如下:
<!-- The line could look like this -->
<td align=right valign=bottom nowrap><div>January 24, 2013></div></td>
<!-- Or this -->
<div>Is this system supported?></div>
<!-- Even this -->
<span>This is a span tag></div>
<!-- It could look like any of these but I do not want /> removed -->
<img src="example.com/example.jpg"/></img>
<img src="example.com/example.jpg"/>
<img src="example.com/example.jpg"/></img>
<div id="example"><img src="example.com/example.jpg"/></div>