正则表达式删除字符之间的 space 但忽略开头的 space
Regex remove space between characters but ignore space at the beginning
我有 HTML 代码,我想用 search/replace 正则表达式清理。我有很多实例,我想用正则表达式删除的单词之间有多个 space,但我希望它忽略每行开头的 HTML 缩进。
表达式 \h{2,4} 删除了 2 到 4 之间的所有 space 但是我怎样才能让它忽略开头的缩进呢?
这是一个示例 HTML 代码:
<tr>
<td><strong>Vamos a sentarnos.</strong></td>
<td><strong>Let's sit down.</strong></td>
</tr>
<tr>
<td>veamos (ver)</td>
<td>let's see (to see)</td>
</tr>
谢谢
https://regex101.com/r/GOxPpS/1
<td>.*?(\h{2,4}).*<\/td>
您可以将 <td>
标签字符串添加到您的正则表达式中。然后搜索总是在标签内。
整个查询解释可以在提供的link:
中找到
<td>
-> matches the characters literally (case sensitive)
.*?
-> matches any character (except for line terminators)
*?
-> Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\h{2,4}
-> matches any horizontal whitespace character. Matches between 2 and 4 times, as many times as possible, giving back as needed (greedy)
看看这是否适合您。如果有 2 个以上的空格并且它们不在行的开头,它将替换任何空格。
(?<!^)\h\K\h+
替换为"nothing"
解释:
(?<!^) # not a previous begin of line
\h # one horizontal space
\K # ignore previous match
\h+ # one or more horizontal spaces
可选方法
([^\n]\h)\h+
替换为 </code></p>
<p><strong><a href="https://regex101.com/r/pq4idK/2" rel="nofollow noreferrer">Demo</a></strong></p>
<p>甚至:<code>([^\n][^\S\r\n])[^\S\r\n]+
(如果不支持\h
)替换为
我有 HTML 代码,我想用 search/replace 正则表达式清理。我有很多实例,我想用正则表达式删除的单词之间有多个 space,但我希望它忽略每行开头的 HTML 缩进。 表达式 \h{2,4} 删除了 2 到 4 之间的所有 space 但是我怎样才能让它忽略开头的缩进呢? 这是一个示例 HTML 代码:
<tr>
<td><strong>Vamos a sentarnos.</strong></td>
<td><strong>Let's sit down.</strong></td>
</tr>
<tr>
<td>veamos (ver)</td>
<td>let's see (to see)</td>
</tr>
谢谢
https://regex101.com/r/GOxPpS/1
<td>.*?(\h{2,4}).*<\/td>
您可以将 <td>
标签字符串添加到您的正则表达式中。然后搜索总是在标签内。
整个查询解释可以在提供的link:
中找到
<td>
-> matches the characters literally (case sensitive)
.*?
-> matches any character (except for line terminators)
*?
-> Quantifier — Matches between zero and unlimited times, as few times as possible, expanding as needed (lazy)
\h{2,4}
-> matches any horizontal whitespace character. Matches between 2 and 4 times, as many times as possible, giving back as needed (greedy)
看看这是否适合您。如果有 2 个以上的空格并且它们不在行的开头,它将替换任何空格。
(?<!^)\h\K\h+
替换为"nothing"
解释:
(?<!^) # not a previous begin of line
\h # one horizontal space
\K # ignore previous match
\h+ # one or more horizontal spaces
可选方法
([^\n]\h)\h+
替换为 </code></p>
<p><strong><a href="https://regex101.com/r/pq4idK/2" rel="nofollow noreferrer">Demo</a></strong></p>
<p>甚至:<code>([^\n][^\S\r\n])[^\S\r\n]+
(如果不支持\h
)替换为