This question already has an answer here:
这个问题在这里已有答案:
- Split a string by commas but ignore commas within double-quotes using Javascript 9 answers
- 用逗号分隔字符串但使用Javascript 9答案忽略双引号中的逗号
Example string:
示例字符串:
"Foo","Bar, baz","Lorem","Ipsum"
Here we have 4 values in quotes separated by commas.
这里我们用引号分隔4个引号值。
When I do this:
我这样做的时候:
str.split(',').forEach(…
than that will also split the value "Bar, baz"
which I don't want. Is it possible to ignore commas inside quotes with a regular expression?
比那还要分割我不想要的“Bar,baz”这个值。是否可以使用正则表达式忽略引号内的逗号?
1 个解决方案
#1
30
One way would be using a Positive Lookahead assertion here.
一种方法是在这里使用Positive Lookahead断言。
var str = '"Foo","Bar, baz","Lorem","Ipsum"',
res = str.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/);
console.log(res); // [ '"Foo"', '"Bar, baz"', '"Lorem"', '"Ipsum"' ]
Regular expression:
正则表达式:
, ','
(?= look ahead to see if there is:
(?: group, but do not capture (0 or more times):
(?: group, but do not capture (2 times):
[^"]* any character except: '"' (0 or more times)
" '"'
){2} end of grouping
)* end of grouping
[^"]* any character except: '"' (0 or more times)
$ before an optional \n, and the end of the string
) end of look-ahead
Or a Negative Lookahead
或者是负面的前瞻
var str = '"Foo","Bar, baz","Lorem","Ipsum"',
res = str.split(/,(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)/);
console.log(res); // [ '"Foo"', '"Bar, baz"', '"Lorem"', '"Ipsum"' ]
#1
30
One way would be using a Positive Lookahead assertion here.
一种方法是在这里使用Positive Lookahead断言。
var str = '"Foo","Bar, baz","Lorem","Ipsum"',
res = str.split(/,(?=(?:(?:[^"]*"){2})*[^"]*$)/);
console.log(res); // [ '"Foo"', '"Bar, baz"', '"Lorem"', '"Ipsum"' ]
Regular expression:
正则表达式:
, ','
(?= look ahead to see if there is:
(?: group, but do not capture (0 or more times):
(?: group, but do not capture (2 times):
[^"]* any character except: '"' (0 or more times)
" '"'
){2} end of grouping
)* end of grouping
[^"]* any character except: '"' (0 or more times)
$ before an optional \n, and the end of the string
) end of look-ahead
Or a Negative Lookahead
或者是负面的前瞻
var str = '"Foo","Bar, baz","Lorem","Ipsum"',
res = str.split(/,(?![^"]*"(?:(?:[^"]*"){2})*[^"]*$)/);
console.log(res); // [ '"Foo"', '"Bar, baz"', '"Lorem"', '"Ipsum"' ]