javascript - I have a question about js merging?
迷茫
迷茫 2017-06-26 10:54:37
0
4
763

I have this one
2|3|4|3|3|2,|3,|2|3,|4,|2|3|3|2|3|2
I want to merge the data with commas like this
2|3|4|3|3|2,3|2|3,4|2|3|3|2|3| 2
Is there any good way?
If there are three or four adjacent, how can I change it? Newbies don’t quite understand! Please answer!

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(4)
Peter_Zhu

,| replaced with ,

typecho

str.replace(',|',',')

为情所困
        问题不是很详细,只是两个相邻并且都有逗号才合并吗?
        var str = '2|3|4|3|3|2,|3,|2|3,|4,|2|3|3|2|3|2';
        var arr = str.split('|');
        for(let i=0;i<arr.length;i++){
            console.log(arr[i])
            if(arr[i].indexOf(',')!=-1&&arr[i+1].indexOf(',')!=-1){
                console.log(arr[i])
                arr.splice(i,2,arr[i]+arr[i+1].split(',')[0]);
                i++
            }
        }
        console.log(arr)
给我你的怀抱

Help you implement it with code.

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>

<script type="text/javascript">
    var str = "2|3|4|3|3|2,|3,|2|3,|4,|2|3|3|2|3|2";
    var result = "";
    for(var i=0; i<str.length; i++){
        if (!isNaN(str[i]) && str[i+1]=="," && str[i+2]=="|" && !isNaN(str[i+3]) && str[i+4]==",") {
            result += str[i];
            result += ",";
            result += str[i+3];
            i = i+4;
        }else{
            result += str[i];
        }
    }
    alert(result);
</script>
</body>
</html>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template