我想在 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
,即第二组中的值,即标签内的内容。
关于javascript - 在 JS 中使用 RegExp 将任何仅包含空格的 html 标签替换为其空格(数量相同),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33303134/