I have this one2|3|4|3|3|2,|3,|2|3,|4,|2|3|3|2|3|2
I want to merge the data with commas like this2|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!
为情所困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)
给我你的怀抱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>