我想在JS中使用RegExp将所有只在其中包含空格(' '
或
)的html标记替换为其html编码的等效标记
。
例如:
代替
'<strong> </strong>' => ' '
另一个例子,
代替:
'<strong> </strong> => ' '
您可以使用如下正则表达式:
str = str.replace(/<(\w+)>((?: |\s)+)<\/\1>/g, '$2');
解释:
<(\w+)> matches the start tag and captures the name
( group to capture the content
(?: |\s)+ matches or whitespace, one or more times
) ends group
<\/\1> matches the end tag with the name of the start tag
匹配将替换为$2
,即第二组中的值,即标签内的内容。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句