You would think this one has been asked before but I cant find it.
I need to separate a js string by un quoted commas. I'm only using double quotes so that should make it a bit simpler.
I have tried two approaches but not nailed it.
I need to turn this:
'body.loaded"who, are , you" div"hello ,"#div-id span CODE, body.loaded span"span, text" code'
into this:
[
'body.loaded"who, are , you" div"hello ,"#div-id span CODE',
'body.loaded span"span, text" code'
]
1) -> match the good parts, which mostly works but gives me allot of empty strings in my result.
'body.loaded"who, are , you" div"hello ,"#div-id span CODE, body.loaded span"span, text" code'.match(
/([^,]*"[^"]*")*/g
)
['body.loaded"who, are , you" div"hello' ,'', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', '', ' body.loaded span"span, text"', '', '', '', '', '', '']
I think its because of the () in the regex.
2) split the bad parts, which isnt quite there yet. The idea here is to match commas followed by an even number of ".
'body.loaded"who, are , you" div"hello ,"#div-id span CODE, body.loaded span"span, text" code'.split(
/,(?![^"]*"[^"]*("[^"]*"[^"]*)*$)/
);
Basically, there has to be a cleaner simpler and more beautiful solution (bear in mind javascript does not support look behinds).