0

I have been searching every where but can't seem to find a solution that fully matches my requirement.

I need to split the following string on a comma but need to exclude the comma between double quotes.

Here is the string that needs splitting.

var s = "36291,362,91,AN - UTS - 36291,0.0050,0.0050,,TRUE,,\"001721,00599\",2017-03-06 00:00,Netherlands Antilles,AN,0,999999999,,,,,,,,";

The required end result needs to be

var a = ['36291','362,91','AN - UTS - 36291','0.0050','0.0050','','TRUE','','\"001721,00599\"','2017-03-06 00:00','Netherlands Antilles','AN','0','999999999','','','','','','','',];

I have been battling for more than a day yet haven't found any joy. There need to be a simple solution that I'm overlooking.

Closest I have come is

var r = s.split(/(".*?"|[^",\s]+)(?=\s*,|\s*$)/);

This works in the regex tester regextester

but not when running it in my browser. None of the suggested results matches my question.

Mark Amery
  • 143,130
  • 81
  • 406
  • 459
Ernie
  • 567
  • 2
  • 6
  • 23

0 Answers0