正则表达式:删除特定标签之间的每两个或更多空格,只留下一个空格

正则表达式:删除特定标签之间的每两个或更多空格,只留下一个空格

我想删除特定标签之间的每两个或更多空格并只留下一个空格:

例如:

<p class="text_obisnuit"> The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you. </p>

我的愿望输出:

<p class="text_obisnuit">The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you.</p>

我尝试了一些方法,但没有效果

(?<=<p class="text_obisnuit">)\s*|\s*(?=</p>)

答案1

这只会删除 2 个或更多空格里面 <p class="text_obisnuit"></p>保留其他多个空格。

  • Ctrl+H
  • 找什么:(?:<p class="text_obisnuit">|\G)(?:(?!</p>).)*?\s\K\s+
  • 用。。。来代替:LEAVE EMPTY
  • 检查环绕
  • 检查正则表达式
  • 请勿检查. matches newline取决于您是否要匹配多行。
  • Replace all

解释:

(?:                         # start non capture group
  <p class="text_obisnuit"> # literally
 |                          # OR
  \G                        # restart from position of last match
)                           # end group
(?:                         # start non capture group
  (?!</p>)                  # negative lookahead, make sure we haven't reach </p>
  .                         # any character
)*?                         # group may appear 0 or more times, not greedy
\s                          # a space
\K                          # forget all we have seen until this position
\s+                         # 1 or more spaces

给定文本:

other     text

<p class="text_obisnuit">  The context of articles,   stories, and conversations helps you     figure out and understand the meaning   of English words in the text that are new to you.   </p>

other    text

给定示例的结果:

other     text

<p class="text_obisnuit"> The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you. </p>

other    text

笔记:它在紧接着<p...>和紧接着之前保留空间</p>


如果您想删除这些空格,您必须运行另一个正则表达式:

  • Ctrl+H
  • 找什么:(?<=<p class="text_obisnuit">)\s+|\s+(?=</p>)
  • 用。。。来代替:LEAVE EMPTY
  • 取消勾选匹配大小写
  • 检查环绕
  • 检查正则表达式
  • Replace all

解释:

(?<=                        # start positive lookbehind, make sure we have 
  <p class="text_obisnuit"> # literally
)                           # end lookbehind
\s+                         # 1 or more spaces
|                           # OR
\s+                         # 1 or more spaces
(?=                         # start positive lookahead
  </p>                      # literally
)                           # end lookahead

给定示例的结果:

other     text

<p class="text_obisnuit">The context of articles, stories, and conversations helps you figure out and understand the meaning of English words in the text that are new to you.</p>

other    text

答案2

HTML 通常不关心空白。如果您显示 HTML,您将看到空白已消失。

我已经为你创建了一个JSFiddle 供测试用。

一个更简单的解决方案是用一个空格替换两个空格并重复尽可能多的次数,但空格实际上并不重要,除非在预格式化的文本中使用 <pre> Tag

答案3

找到解决方案:

搜索:

(?s)(\G|<p class="text_obisnuit">)((?!</p>).)*?\K((?<=>)\h+|\h+(?=<|\h))

替换为:

(leave empty)

相关内容