这里有两种方法。
#1
可以匹配正则表达式
[^"\r\n]+(?:"[^"]*"[^"\r\n]+)*
Demo
表达式可以分解如下。
[^"\r\n]* # match zero or more characters other than those in the
# character class
(?: # begin non-capture group
"[^"]*" # match double-quote followed by zero or more characters
# other than a double-quote, followed by a double-quote
[^"\r\n]+ # match zero or more characters other than those in the
# character class
)* # end non-capture group and execute it zero or more times
#2
匹配不在双引号之间的行终止符等效于匹配行终止符,从字符串的开头开始,前面有偶数个双引号。您可以使用以下正则表达式匹配此类行终止符(未设置多行标志,以便^ 匹配字符串的开头,而不是行的开头)。
/(?<=^[^"]*(?:"[^"]*"[^"]*)*)\r?\n/
Start your engine!
Javascript 的正则表达式引擎(令人印象深刻地支持可变长度的lookbehinds)执行以下操作。
(?<= : begin positive lookbehind
^ : match beginning of string (not line)
[^"]* : match 0+ chars other than '"'
(?: : begin non-capture group
"[^"]*" : match '"', 0+ chars other than '"', '"'
[^"]* : match 0+ chars other than '"'
)* : end non-capture group and execute 0+ times
) : end positive lookbehind
\r?\n : match line terminator