For string:
\'29 July, 2014, 30 July, 2014, 31 July, 2014\'
How can I split on every second comma in the string? So that my results are:
You can split with a regular expression. For example:
var s = '29 July, 2014, 30 July, 2014, 31 July, 2014';
s.split(/,(?= \d{2} )/)
returns
["29 July, 2014", " 30 July, 2014", " 31 July, 2014"]
This works OK if all your dates have 2 digits for the day part; ie 1st July will be printed as 01 July.
The reg exp is using a lookahead, so it is saying "split on a comma, if the next four characters are [space][digit][digit][space]".
Edit - I've just improved this by allowing for one or two digits, so this next version will deal with 1 July 2014 as well as 01 July 2014:
s.split(/,(?= \d{1,2} )/)
Edit - I noticed neither my nor SlyBeaver's efforts deal with whitespace; the 2nd and 3rd dates both have leading whitespace. Here's a split solution that trims whitespace:
s.split(/, (?=\d{1,2} )/)
by shifting the problematic space into the delimiter, which is discarded.
["29 July, 2014", "30 July, 2014", "31 July, 2014"]