0

In my javascript code, I am parsing csv file and making json data later sending it to mongodb insert. During csv parsing i am facing some problem when any csv cell is empty or null. For example when data is

"abc", "xyz",,,"axbycz"

my expectation is "abc", "xyz", null, null, "axbycz" but i am getting only "abc", "xyz", "axbycz".

Here is my actual data and regex to parse it.

Data:

263P,,,Madanayakanahalli,xys,abc,00:35 Min.,,
"[
  {
   "busstop": "MG Road", 
   "latlons": ["1.0455367265411", "17.5055545144687"]
   }
 ]",
 "08:35,  17:25","09:10,  18:00","07:55,  16:45","08:30, 15:35, 17:20"

Regex:

var cells = rows[i].match(/("\[.*?\]"|".*?"|".*?"\r|[^",]+)(?=,|$)/g);

This regex is working well if all cells have some data.

Please someone help me to get the csv parsing as per my expectation.

Shashi
  • 1
  • 2
  • If i answer your query using loop statement instead of regex, will that be fine??? – Shiladitya Jul 10 '17 at 12:17
  • Possible duplicate of [Regex to split a CSV](https://stackoverflow.com/questions/18144431/regex-to-split-a-csv) – Yosef Weiner Jul 10 '17 at 12:22
  • i used regex because i have to parse huge data. I case of loop it may be time taking. But no issue, please suggest me the solution. – Shashi Jul 10 '17 at 12:23

0 Answers0