1

Say I have a string like this D8FCB499-47F82E3F-B458F094-896C526F and I wish to format it so it ends up in the following format D8FCB499-47F8-2E3F-B458-F094896C526F three sets of 4 letters in the middle. I was thinking of turning it into an array and using push("-") at certain indexes but I feel like there's a much easier way of doing this but I can't for the love of me figure it out

var input1 = document.getElementById("input1");
var input2 = document.getElementById("input2");
var newID = [];
formatString = function () {
    var idnumber = input1.value;
    console.log(idnumber.length);
    for (i = 0; i < idnumber.length; i++)

        console.log(idnumber[i], " has index " + i);
    newID.push(idnumber[i]);
    // orignal string D8FCB499-47F82E3F-B458F094-896C526F
    // formated string D8FCB499-47F8-2E3F-B458-F094896C526F
    input2.value = newID;
    console.log(newID);
}
gogosimba
  • 65
  • 7
  • 1
    Dupe is pretty sucky, Regular expression `"D8FCB499-47F82E3F-B458F094-896C526F".replaceAll("-",'').replace(/^(.{8})(.{4})(.{4})(.{4})/,'$1-$2-$3-$4-');` – epascarello Nov 21 '22 at 13:01

0 Answers0