Regular replacement of a specified range of strings

make * substitutions for the specified range of elements.

is there a better regular way to hide numbers?
writes how to handle a string. The demo is as follows:

// 
hideCertificate(number) {
    let a = "12345678";
    let len = a.length;
    let res;
    res = `${a.substr(0, len / 2 / 2)}${new Array(Math.floor(len / 2)).fill("*").join("")}${a.substr(-(len / 2 / 2))}`;
    console.log(res);
    // 12****78
},
Apr.15,2022

res = a.replace (/ ^ (\ d {2}) (. *?) (\ d {2}) $/, (match, p1, p2, p3) = > p1+p2.replace (/. / g, "*") + p3)


replace(/^((\d{2})\d+(\d{2}))$/, '$2****$3')

let len=a.length
let reg=`\\d{${~~(len/4)}}`
a.replace(new RegExp(`^(${reg})(\\d*)(${reg})$`),`$1${"*".repeat(len/2)}$3`)
Menu