csv数据基于逗号分割,但忽略双引号或任何括号

时间:2021-12-07 03:46:27

I need to parse a .csv file in javascript and split the fields based on comma but it should ignore the commas within double quotes or any kind of brackets. Here is the snippet of my data.

我需要在javascript中解析.csv文件并根据逗号分割字段,但它应该忽略双引号或任何类型括号内的逗号。这是我的数据片段。

"263P", "12 KMS","http://www.someurl.com/color?width=10&height=70&iframe=true","[{"stop": "abc, def, A.B.C", "latlons": ["13.0455367265411", "77.5055545144687"]}]", "08:35,09:55,11:40", "13:00,  14:20,  15:40,  17:25"

I have tried many options which are suggested on * but those are not working for my data.

我已经尝试了很多在*上建议的选项,但这些选项对我的数据不起作用。

one of the very near matching regex is

一个非常接近匹配的正则表达式是

str.match(/(".*?"|[^,]+)(?=,|$)/g);

but it is not working properly within double quotes

但它在双引号内无法正常工作

1 个解决方案

#1


0  

i could form the regex for my own question so posting the solution here.

我可以为我自己的问题形成正则表达式,所以在这里发布解决方案。

str.match(/("[.?]"|".?"|".*?"\r|[^",]+)(?=,|$)/g)

with this regex i am able to separate all the csv cells based on comma (ignoring within quotes).

使用此正则表达式,我能够基于逗号分隔所有csv单元格(忽略引号内)。

#1


0  

i could form the regex for my own question so posting the solution here.

我可以为我自己的问题形成正则表达式,所以在这里发布解决方案。

str.match(/("[.?]"|".?"|".*?"\r|[^",]+)(?=,|$)/g)

with this regex i am able to separate all the csv cells based on comma (ignoring within quotes).

使用此正则表达式,我能够基于逗号分隔所有csv单元格(忽略引号内)。