Split on every second comma in javascript

2020-04-03 09:32发布

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

标签: javascript
6条回答
家丑人穷心不美
2楼-- · 2020-04-03 10:15

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++]);
}
查看更多
放我归山
3楼-- · 2020-04-03 10:18

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)
查看更多
\"骚年 ilove
4楼-- · 2020-04-03 10:23

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"]
查看更多
混吃等死
5楼-- · 2020-04-03 10:23

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);
查看更多
虎瘦雄心在
6楼-- · 2020-04-03 10:34

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
查看更多
Melony?
7楼-- · 2020-04-03 10:39

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
*/
查看更多
登录 后发表回答