1

I have the following line which splits correctly but produces a duplicate for "abc"

console.log("squadName, \"abc\"".split(/,\s*(?=([^"]*"[^"]*")*[^"]*$)/))

The expected output should be the same as that of

console.log("squadName, \"abc\"".split(/,\s*/))

However, I'm trying to ignore commas within quotes, as explained in A regex to match a comma that isn't surrounded by quotes

deceze
  • 510,633
  • 85
  • 743
  • 889
Kaustubh Badrike
  • 495
  • 1
  • 4
  • 18

1 Answers1

2

Parentheses in a JS regular expression explicitly create a capture group. If you prepend the contents with ?:, it will instead be a non-capturing group. If you see (? in general this is usually a special kind of paren-grouping value.

For your code, you'd want to do:

console.log("squadName, \"abc\"".split(/,\s*(?=(?:[^"]*"[^"]*")*[^"]*$)/))
Kaustubh Badrike
  • 495
  • 1
  • 4
  • 18
loganfsmyth
  • 156,129
  • 30
  • 331
  • 251