正規表示式:刪除 html 標記上的重複雙引號(撇號)(來自字串)

正規表示式:刪除 html 標記上的重複雙引號(撇號)(來自字串)

我有這個 html 標籤(來自字串):

<meta name="description" content="I love my mother" but I love my sister" more than I can say"/>

正如你所看到的,我在其中有 4 個雙引號(撇號)內容部分。應該只有 2 個雙引號:一個在開頭content=",一個在結尾"/>

我必須找到所有包含其他雙引號(除了內容部分中的 2 個雙引號)的標籤,並將它們刪除:

輸出應該是:

<meta name="description" content="I love my mother but I love my sister more than I can say"/>

我做了一個正規表示式,但不太好。也許你可以幫助我:

尋找:(?-s)(<meta name="description" content=")(*?\K.*"(?s))"/>

替換為:\1\2

答案1

這是一個方法:

  • Ctrl+H
  • 找什麼:(?:<meta name="description" content="|\G(?!^))[^"]*\K"(?=.*?"/>)
  • 用。LEAVE EMPTY
  • 查看 環繞
  • 查看 正規表示式
  • 取消選取 . matches newline
  • Replace all

解釋:

(?:             # non capture group
  <meta name="description" content="      # literally
 |               # OR
  \G(?!^)         # restart from last match position (not at the beginning of a line)
)               # end group
[^"]*           # 0 or more non quote
\K              # forget all we have seen until this position
"               # a double quote
(?=.*?"/>)      # positive lookahead, make sure we have "/>  somewhere after

截圖(之前):

在此輸入影像描述

截圖(之後):

在此輸入影像描述

示範和解釋

相關內容