Split on every second comma in javascript

后端 未结 6 1101
余生分开走
余生分开走 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:31

    Like this

     var str = '29 July, 2014, 30 July, 2014, 31 July, 2014'
    
     var parts = str.split(',')
    
     var answer = []
    
     for (var i=0; i<parts.length; i++) {
            if (i < 1) continue;
            if (i % 2 == 1) {
               answer.push(parts[i-1] + ',' + parts[i]);
            }
     }
    
     console.log(answer)
    
    0 讨论(0)
  • 2021-01-18 01:37

    Not sure if you can do it in a one-liner, but a workaround would be to split on every comma then join consecutive values.

    ex:

    var str = '29 July, 2014, 30 July, 2014, 31 July, 2014';
    var list = str.split(",");
    var list2 =[];
    for (var i=0;i<list.length; i+=2){
        list2.push(list[i]+","+list[i++]);
    }
    
    0 讨论(0)
  • 2021-01-18 01:43

    I know it's almost two years ago.

    This function is reusable.

    String.prototype isn't necessary to make it work.. But it's easy..

    String.prototype.splitEvery = function ( splitter, every ){
    
        var array = this.split( splitter), newString = '', thisSplitter; 
    
        $.each( array, function( index, elem ){ 
    
            thisSplitter = ( index < array.length - 1 || index % every === 0 ) ? '' : splitter;
    
            newString += elem + thisSplitter;                                                               
        });          
    
        return newString;                                                                                   
    };  
    
    
    var dateString = '29 July, 2014, 30 July, 2014, 31 July, 2014';
    

    The usage in this case is:

    dateString.splitEvery( ',', 2 );
    

    First parameter is the split, second is how many is between each.

    Result are:

    29 July 2014, 30 July 2014, 31 July 2014
    
    0 讨论(0)
  • 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
    */
    
    0 讨论(0)
  • 2021-01-18 01:48

    This is not a refactored solution, but it works:

    var $dates = '29 July, 2014, 30 July, 2014, 31 July, 2014';
    $dates = $dates.split(',');
    $result = [];
    $dateValue = '';
    
    for($i=0 ; $i<$dates.length ; $i++) {
    if($i % 2 != 0 && $i > 0) {
                $dateValue += ','+$dates[$i];
        $result.push($dateValue);
        $dateValue = '';
    } else {
        $dateValue += $dates[$i];
    }
    }
    
    console.log($result);
    
    0 讨论(0)
  • 2021-01-18 01:52

    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"]
    
    0 讨论(0)
提交回复
热议问题