0

im parsing a csv file with javascript, that file styles the cells with commas in the numbered values. when parsing it to javascript i get the entire row as one string where the cells are divided by commas, as such:

2, some text,9740127,"₪ 38,850.00","₪ 5,550.00","₪ 28,305","₪ 4,995",22/05/2020

in order to parse the files and create an object from every row i need to match the commas that are Not part of a number, so the end result is:

2
some text
9740127
₪ 38,850.00
₪ 5,550.00
₪ 28,305
₪ 4,995
22/05/2020

i tried matching the commas inside the numbers in hopes to negate it afterwards with that regex:(".\s\d+,\d+[.]?[\d]+") but i cant seem to target the other commas instead afterwards.

i feel like im close but i cant seem to figure it out, would appreciate the help! Thanks

2 Answers2

1

Assuming the double quotes are in the right format, one option might be to split on a comma while asserting what is on the right is not " followed by either a comma or the end of the string.

, *(?!(?:[^"]|\\")*"(?:,|$))

Regex demo

let str = '2, some text,9740127,"₪ 38,850.00","₪ 5,550.00","₪ 28,305","₪ 4,995",22/05/2020'

console.log(str.split(/, *(?!(?:[^"]|\\")*"(?:,|$))/g));
The fourth bird
  • 154,723
  • 16
  • 55
  • 70
0

You can use Javascript's .split() method for your use-case.

var mystring = '2, some text,9740127,"₪ 38,850.00","₪ 5,550.00","₪ 28,305","₪ 4,995",22/05/2020';
var splitStringArray = mystring.split(",");
    
for(var i = 0; i < splitStringArray.length; i++) {
  console.log(splitStringArray[i]);
}
Rene
  • 976
  • 1
  • 13
  • 25
Ayman Arif
  • 1,456
  • 3
  • 16
  • 40
  • it is not working because it will split the number with commas too – Rene May 29 '20 at 11:32
  • 1
    thats the naive approach and i initially tried it, but it splits numbers with commas as well which effects the validity of the data. – Lior Bitton May 29 '20 at 12:25