[英]Javascript replace every 6th colon in array
I have some problems with replacing every 6th colon in my array. 我在替换数组中的第6个冒号时遇到了一些问题。 Have tried something with Regex, but that doesn't seem to work.
已经用Regex尝试了一些东西,但这似乎不起作用。 I have red other questions were people are using nth and then set this variabele to the index you want to replace, but can't figure out why that isn't working.
我还有其他问题,就是人们正在使用nth,然后将此variabele设置为要替换的索引,但无法弄清楚为什么它不起作用。 I used the join function to replace the ',' in my array with ':'.
我使用join函数将数组中的','替换为':'。
arrayProducts[i] = arrayProducts[i].join(':');
When i use console.log(arrayProducts);
当我使用
console.log(arrayProducts);
this is my result: 这是我的结果:
F200:0:0.0000:1:100:0:1:KPO2:0:0.0000:1:200:0:2:HGB1:0:0.0000:1:300:0:3
This is what I want: 这就是我要的:
F200:0:0.0000:1:100:0:1,KPO2:0:0.0000:1:200:0:2,HGB1:0:0.0000:1:300:0:3
Edit: F200, KP02 and HGB1, could also be numbers / digits like: 210, 89, 102 so the :[AZ] method from regex doesn't work. 编辑:F200,KP02和HGB1,也可能是数字/数字,例如:210、89、102,因此来自正则表达式的:[AZ]方法不起作用。
You can just count the number of colon occurences and replace every nth of them. 您可以只计算冒号发生的数量并替换每n个冒号。
var str = 'F200:0:0.0000:1:100:0:1:KPO2:0:0.0000:1:200:0:2:HGB1:0:0.0000:1:300:0:3', counter = 0; res = str.replace(/:/g, function(v) { counter++; return !(counter % 7) ? ',' : v; }); console.log(res);
A regex solution is viable. 正则表达式解决方案是可行的。 You can use a function as the second parameter of the .replace method to make full use of backreferences.
您可以将函数用作.replace方法的第二个参数,以充分利用反向引用。
var str = 'F200:0:0.0000:1:100:0:1:KPO2:0:0.0000:1:200:0:2:HGB1:0:0.0000:1:300:0:3'; str = str.replace(/((?:[^:]*:){6}(?:[^:]*)):/g, function() { var matches = arguments; return matches[1] + ','; }); console.log(str);
What you are looking for is to split over the following expression :[AZ]
(assuming that your rows always start with this range) 您正在寻找的是分割以下表达式
:[AZ]
(假设您的行始终以该范围开头)
a simple solution could be: 一个简单的解决方案可能是:
mystring.split(/:[A-Z]/).join(',')
/:[AZ]/
matches any :
followed by a uppercase letter /:[AZ]/
匹配任何:
后跟一个大写字母
maybe you can try this approach, loop your array and join it manually, something like : 也许您可以尝试这种方法,循环数组并手动加入它,例如:
var strarr = "F200:0:00000:1:100:0:1:KPO2:0:00000:1:200:0:2:HGB1:0:00000:1:300:0:3";
var arr = strarr.split(":")
var resStr = "";
for(var i = 0; i < arr.length; i++)
{
if(i > 0 && i%7 == 0)
resStr = resStr + "," + arr[i]
else
resStr = resStr + ( resStr == "" ? "" : ":") + arr[i];
}
console.log(resStr);
You could use replace with a look for six parts with colon and replace the seventh. 您可以使用replace并用冒号查找六个部分,然后替换第七个部分。
var string = 'F200:0:0.0000:1:100:0:1:KPO2:0:0.0000:1:200:0:2:HGB1:0:0.0000:1:300:0:3', result = string.replace(/(([^:]*:){6}[^:]*):/g, '$1,'); console.log(result);
Another solution (based on the number of iteration) 另一个解决方案(基于迭代次数)
using map method: 使用地图方法:
str.split(':').map((v, i) => (i % 7 === 0 ? ',' : ':') + v ).join('').slice(1)
using reduce method: 使用减少方法:
str.split(':').reduce((acc,v, i) => {
return acc + (i % 7 === 0 ? ',' : ':' ) + v ;
}, '').slice(1)
Note: arrow expression does not work on old browsers 注意:箭头表达式在旧的浏览器上不起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.