0

This is what I came up so far

 var split_string = string_a.split(/(?:"|",")/);

however for

var string_a ='"ldfssdf","mom , ddd"';

its not giving the right result in the array what i basically want is this

 string_a = ["ldfssdf", "mom, ddd"];
user1493786
  • 325
  • 1
  • 3
  • 13
  • 1
    Isn't this question about parsing CSV? http://stackoverflow.com/a/10290562/716443 – DavidO Jan 14 '15 at 07:12
  • split based on comma followed by double quote. `,(?=")` [DEMO](https://regex101.com/r/iY8wV7/1) – Braj Jan 14 '15 at 07:16
  • Splitting on comma followed by double quote works for the example given, but the point to quoting CSV fields is to allow for escaped commas and newlines. Splitting on comma followed by a quote would break on non-quoted fields, as well as the edge case of fields where a comma immediately precedes the field's closing quote. ...and that still doesn't deal with embedded newlines. – DavidO Jan 14 '15 at 07:30
  • I sort of figured a simpler way to do it. By splitting on (/","|"/g), and then splicing off the first and last item of the array as its just empty string. I don't know if there will be something wrong in it? – user1493786 Jan 14 '15 at 18:16

2 Answers2

1
,(?=(?:[^"]*"[^"]*")*[^"]*$)

Split by this.See demo.

https://regex101.com/r/fA6wE2/11

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  ,                        ','
--------------------------------------------------------------------------------
  (?=                      look ahead to see if there is:
--------------------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                         times (matching the most amount
                         possible)):
--------------------------------------------------------------------------------
      [^"]*                    any character except: '"' (0 or more
                           times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
      "                        '"'
--------------------------------------------------------------------------------
      [^"]*                    any character except: '"' (0 or more
                           times (matching the most amount
                           possible))
--------------------------------------------------------------------------------
      "                        '"'
--------------------------------------------------------------------------------
    )*                       end of grouping
--------------------------------------------------------------------------------
    [^"]*                    any character except: '"' (0 or more
                         times (matching the most amount
                         possible))
--------------------------------------------------------------------------------
    $                        before an optional \n, and the end of
                         the string
--------------------------------------------------------------------------------
  )                        end of look-ahead
vks
  • 67,027
  • 10
  • 91
  • 124
  • Wow. Very nicely crafted regex. The only issue I see with it is that it leaves the array a little dirty leaving behind the leading and trailing `"` characters. You could maybe correct it by adding `.map(function(a){return a.replace(/^"|"$/g, '')})` or more simply `.map(function(a){return a.replace(/"/g, '')})` to the resulting array. I don't see a way to do it with one regex statement because your splitting on the regex match. If you could manage to match the quotes, it would still result in an array with leading and trailing empty values (e.g. `["","ldfssdf","mom , ddd",""]`). – Joseph Marikle Jan 14 '15 at 07:34
0

Here is what you want to do.

Check this link out: http://jsfiddle.net/duongtuanluc/q8z7fkgx/

Check this link out: http://jsfiddle.net/duongtuanluc/q8z7fkgx/1/ (If contains empty value "")

var string_a ='"ldfssdf","mom , ddd"';
var rgx = /(\"[\s\S]+?\")/g;
var arr = string_a.match(rgx);
console.log(arr);

enter image description here

Luc
  • 2,800
  • 2
  • 25
  • 46