I'm so sorry if I missed details on the 1st post which is closed. I added comma within double quotes to the string, and I added the string into variable. I cannot simply split on comma.
Original Post
Basically, I need to split a line into an array. But I encountered error when 'double quote' inside 'double quotes'. The sample line is like this:
var line = '"123","str","456",8/10/2021 7:44:47 AM,"str","str","str","","str","str",0,"789",1,"101112","Ironman"s, iPhone","+131415",30'
"123","str","456",8/10/2021 7:44:47 AM,"str","str","str","","str","str",0,"789",1,"101112","Ironman"s, iPhone","+131415",30 My current regex is:
values = line.split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/g);
My testing tool is https://regexr.com/, and I'm not sure how to create a colored explain graph which I saw other's post. If you could point out as well please.
If I remove the double quote inside to "Ironman s, iPhone', my regex works well, but it's a big headache now as I cannot find a working way to handle.
You were almost there. You just needed to replace the $
with (?:,|$)
because at every ,
split position there won't necessarily be an even number of double quotes to the end of the line.
,(?=(?:[^"]*"[^"]*")*[^"]*(?:,|$))
For example,
const line = '"123","str","456",8/10/2021 7:44:47 AM,"str","str","str","","str","str",0,"789",1,"101112","Ironman"s, iPhone","+131415",30';
const values = line.split(/,(?=(?:[^"]*"[^"]*")*[^"]*(?:,|$))/g);
console.log(values);