pjmil pjmil - 7 months ago 17
Javascript Question

Split on every second comma in javascript

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:

[0] => 29 July, 2014
[1] => 30 July, 2014
[2] => 31 July, 2014

Answer

Or this:

var text='29 July, 2014, 30 July, 2014, 31 July, 2014';
result=text.match(/([0-9]+ [A-z]+, [0-9]+)/g);

UPD: You can use this regExp to a find all matches:

//    result=text.match(/[^,]+,[^,]+/g);

    var text='string1, string2, string3, string4, string5, string 6';
    result=text.match(/[^,]+,[^,]+/g);

/*
result: 
string1, string2
string3, string4
string5, string 6
*/
Comments