正则表达式匹配(替换)跨度标签之间单词中所有出现的双引号

Regex match (replace) all occurrences of double quotes in words between span tags

我正在尝试替换两个跨度标签之间出现的所有 "。

我使用:

(?<=<span>[a-zA-Z0-9_æøåÆØÅ_,.;:!#€%&\/()$§'])*(\")(?=[a-zA-Z0-9_æøåÆØÅ_,.;:!#€%&\/()$§']*<\/span>)

查找字母+特殊字符

找到“

前瞻字母+specialChars

但是使用 html 字符串

<span>d"s"s"</span>

它只匹配最后一次出现的“

如何匹配(最终替换)标签中出现的所有双引号?

提前致谢。

使用

/(?<=<span>[^<>]*)"(?=[^<>]*<\/span>)/g

参见regex proof

解释

--------------------------------------------------------------------------------
  (?<=                     look behind to see if there is:
--------------------------------------------------------------------------------
    <span>                   '<span>'
--------------------------------------------------------------------------------
    [^<>]*                   any character except: '<', '>' (0 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of look-behind
--------------------------------------------------------------------------------
  "                        '"'
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    [^<>]*                   any character except: '<', '>' (0 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
    <                        '<'
--------------------------------------------------------------------------------
    \/                       '/'
--------------------------------------------------------------------------------
    span>                    'span>'
--------------------------------------------------------------------------------
  )                        end of look-ahead

别在意后面的样子。相反,匹配 " where </span> followed without finding <span> earlier than </span>,即 "inside 一个跨度open/close对:

"(?=((?!<span>).)*<\/span>)

参见 live demo

分解正则表达式:

  • " 文字引用
  • (?!<span>). 除了 <span>
  • < 之外的任何字符
  • ((?!<span>).)* 任何字符,但不包括 <span>
  • <
  • (?=((?!<span>).)*<\/span>) 后跟在 <span>
  • 之前遇到 </span> 的输入