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
  • 491,798
  • 79
  • 706
  • 853
Kaustubh Badrike
  • 354
  • 1
  • 2
  • 14

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
  • 354
  • 1
  • 2
  • 14
loganfsmyth
  • 146,797
  • 27
  • 317
  • 241