Split on every second comma in javascript

后端 未结 6 1103
余生分开走
余生分开走 2021-01-18 01:05

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:

6条回答
  •  盖世英雄少女心
    2021-01-18 01:45

    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
    */
    

提交回复
热议问题