假设我有一个包含以下内容的文档:
<div data-test-id="comment">
<div class="class1
class2
class3">
<p class="class1">
Lorem Ipsum
</p>
</div>
第二行和第三行有一个 HTML 标签,该标签被分成两行。我一直无法弄清楚如何将它们合并到一行,如下所示:
<div data-test-id="comment">
<div class="class1 class2 class3">
<p class="class1">
Lorem Ipsum
</p>
</div>
每当我尝试使用“?”非贪婪修饰符时,它似乎都会忽略它。
答案1
- Ctrl+H
- 找什么:
(?:<\w+|\G)(?:(?!>).)*?\K\s+
- 替换为:
#1 空格
- 查看 环绕
- 查看 正则表达式
- 取消选中
. matches newline
- Replace all
解释:
(?: # non capture group
< # literally <
\w+ # 1 or more word character (i.e. tag name)
| # OR
\G # restart from last match position
) # end group
(?: # non capture group (non greedy token)
(?!>) # negative lookahead, make sure we haven't < after
. # any character
)*? # end group, may appear 0 or more times, not greedy
\K # forget all we have seen until this position
\s+ # 1 or more any kind of spaces
截图(之前):
截图(之后):