我确实看到了SO的另一种解决方案,但是它使整个文件没有新行。我只需要一个没有新行的特定部分。
我正在尝试删除这些标签之间的间距,以使它们彼此紧挨着,例如:
<a rel="nofollow noopener" style="display:inline; text-decoration: none;" href=""><img border="0" class="full" height="auto" width="300" alt="" src="http://via.placeholder.com/600x338"></a><a rel="nofollow noopener" style="display:inline; text-decoration: none;" href=""><img border="0" class="full" height="auto" width="300" alt="" src="http://via.placeholder.com/600x338"></a><a rel="nofollow noopener" style="display:inline; text-decoration: none;" href=""><img border="0" class="full" height="auto" width="300" alt="" src="http://via.placeholder.com/600x338"></a><a rel="nofollow noopener" style="display:inline; text-decoration: none;" href=""><img border="0" class="full" height="auto" width="300" alt="" src="http://via.placeholder.com/600x338"></a>
它们当前如下所示:
<a rel="nofollow noopener" style="display:inline; text-decoration: none;" href="">
<img border="0" class="full" height="auto" width="300" alt="" src="http://via.placeholder.com/600x338">
</a>
<a rel="nofollow noopener" style="display:inline; text-decoration: none;" href="">
<img border="0" class="full" height="auto" width="300" alt="" src="http://via.placeholder.com/600x338">
</a>
<a rel="nofollow noopener" style="display:inline; text-decoration: none;" href="">
<img border="0" class="full" height="auto" width="300" alt="" src="http://via.placeholder.com/600x338">
</a>
<a rel="nofollow noopener" style="display:inline; text-decoration: none;" href="">
<img border="0" class="full" height="auto" width="300" alt="" src="http://via.placeholder.com/600x338">
</a>
答案 0 :(得分:1)
这是我在注释中建议的正则表达式的示例,我添加了\t
和\s{2,}
来拉出制表符和2个或更多空格字符。
let temp = `<a rel="nofollow noopener" style="display:inline; text-decoration: none;" href="">
<img border="0" class="full" height="auto" width="300" alt="" src="http://via.placeholder.com/600x338">
</a>
<a rel="nofollow noopener" style="display:inline; text-decoration: none;" href="">
<img border="0" class="full" height="auto" width="300" alt="" src="http://via.placeholder.com/600x338">
</a>
<a rel="nofollow noopener" style="display:inline; text-decoration: none;" href="">
<img border="0" class="full" height="auto" width="300" alt="" src="http://via.placeholder.com/600x338">
</a>
<a rel="nofollow noopener" style="display:inline; text-decoration: none;" href="">
<img border="0" class="full" height="auto" width="300" alt="" src="http://via.placeholder.com/600x338">
</a>`;
console.log(temp.replace(/[\r\n|\r|\n|\t]/g, '').replace(/\s{2,}/g, ' '))
答案 1 :(得分:0)
此模式应该有效:(?<=>)\s*|\s*(?=<a)
您可以看到一个演示here。只需将匹配项替换为""