I have a string, which always has a lenght of 32:
var str = "34v188d9cefa401f988563fb153xy04b";
Now I need to add a minus (-) after following number of characters:
Output should be:
34v188d9-cefa-401f-9885-63fb153xy04b
So far I have tried different calculations with e.g.:
str.split('').reduce((a, b, c) => a + b + (c % 6 === 4 ? '-' : ''), '');
But I don't get the expected result.
You could use a regex replacement:
var str = "34v188d9cefa401f988563fb153xy04b";
var output = str.replace(/^(.{8})(.{4})(.{4})(.{4})(.{12})$/, "$1-$2-$3-$4-$5");
console.log(output);
If you want to use reduce you can do something like this
var str = "34v188d9cefa401f988563fb153xy04b";
const result = str.split('').reduce((res, c, i) => {
const hiphensPositions = [7, 11, 15, 19]
return res + c + (hiphensPositions.includes(i)?'-':'')
}, '')
console.log(result)
var str = "34v188d9cefa401f988563fb153xy04b";
var a=""
for (let i in str){if (i == 7 || i==11 || i==15 || i ==
19){a = a+str[i]+"-"} else{a+=str[i] }}
should do it.