search

Home  >  Q&A  >  body text

javascript - I have a question about js merging?

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!

迷茫迷茫2769 days ago768

reply all(4)I'll reply

  • 天蓬老师

    天蓬老师2017-06-26 10:56:37

    ,| replaced with ,

    reply
    0
  • typecho

    typecho2017-06-26 10:56:37

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

    reply
    0
  • 为情所困

    为情所困2017-06-26 10:56:37

            问题不是很详细,只是两个相邻并且都有逗号才合并吗?
            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)

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-06-26 10:56:37

    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>

    reply
    0
  • Cancelreply