简体   繁体   English

正则表达式以匹配双引号忽略双引号

[英]Regex to Match Quoted Strings Ignoring Double Quotes

I'm trying to match quoted strings in an Excel style formula: 我试图匹配Excel样式公式中带引号的字符串:

= "Red" & "Blue" & "Green"

The following regex finds the matches nicely: 以下正则表达式可以很好地找到匹配项:

".*?"

Where I'm having problems though is where the match contains Excel/VBScript escaped quotes (two quotes together): 我遇到问题的地方是匹配项包含Excel / VBScript转义引号(两个引号在一起):

= "Red" & "Blue" & """Green"" as grass"

I want the double quotes captured inside the one match so I end up with: "Red" , "Blue" , """Green"" as grass" . 我希望在一个匹配项中捕获双引号,所以我最终得到: "Red""Blue""""Green"" as grass"

I'm doing this in JavaScript. 我正在用JavaScript执行此操作。

It seems like this regex will suit you: 这个正则表达式似乎适合您:

/"(?:[^"]|"")*"/g

Explanation: 说明:

  1. A susbtring that starts with a double quote 以双引号开头的怀疑
  2. With anything not quote or double double quote inside 里面没有引号或双引号
  3. Followed by a quote char 后跟报价字符

JSFiddle: http://jsfiddle.net/S22Qe/ JSFiddle: http : //jsfiddle.net/S22Qe/

Regex101: http://regex101.com/r/mJ0pX0/1 Regex101: http: //regex101.com/r/mJ0pX0/1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM