2

I have some problems with replacing every 6th colon in my array. Have tried something with Regex, but that doesn't seem to work. 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. I used the join function to replace the ',' in my array with ':'.

arrayProducts[i] = arrayProducts[i].join(':');

When i use 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

Thanks for reading!

Edit: F200, KP02 and HGB1, could also be numbers / digits like: 210, 89, 102 so the :[A-Z] method from regex doesn't work.

Koen van de Sande
  • 223
  • 1
  • 5
  • 14

6 Answers6

2

You can just count the number of colon occurences and replace every nth of them.

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);
kind user
  • 40,029
  • 7
  • 67
  • 77
1

A regex solution is viable. You can use a function as the second parameter of the .replace method to make full use of backreferences.

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);
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105
Scoots
  • 3,048
  • 2
  • 21
  • 33
0

What you are looking for is to split over the following expression :[A-Z] (assuming that your rows always start with this range)

a simple solution could be:

mystring.split(/:[A-Z]/).join(',')

/:[A-Z]/ matches any : followed by a uppercase letter

amd
  • 20,637
  • 6
  • 49
  • 67
0

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);
NomNomNom
  • 811
  • 3
  • 12
  • 37
0

You could use replace with a look for six parts with colon and replace the seventh.

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);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

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

amd
  • 20,637
  • 6
  • 49
  • 67