我目前正在使用.replace(/>\s+</g,'><')
删除HTML标记之间的空格。
这将替换:
<hello> a b c </hello> <world> x y z </world>
与:
<hello> a b c </hello><world> x y z </world>
我想更新此功能以获得以下结果:
<hello>a b c</hello><world>x y z</world>
我将如何改写此正则表达式以去除包围内部标签内容的新行,空格和制表符?
最佳答案
尝试这样的事情:
"<hello> a b c </hello> <world> x y z </world>".replace(/\s*(<\/?.*?\/?>)\s*/g, "$1")
印刷品:
"<hello>a b c</hello><world>x y z</world>"
说明:
\s*
-标记前0或n个空格(<\/?.*?\/?>)
-已捕获的标签,<...>
,</...>
或<.../>
\s*
-标记后0或n个空格捕获的标签被放回字符串中减去未捕获的匹配空格