in the following code, I want to complete the number after "p" to 3 digits, but I find that no matter how many bits are after p, there is always a 0 in front of it.
var a="18917304_p1234";
a.replace(/(\d.*p)(\d.*)/,"$1"+("$2".padStart(3, "0")));
// "18917304_p01234"
after my random analysis, it may be because padStart
does not get the value of $2
, but treats $2
as a normal string. The word $2
is 2 in length, so padStart
always makes up a zero.
later, it was solved by a different way of writing, but if you don"t rewrite it, is there a more direct way to deal with this situation?
a.replace(/(\d.*p)(\d.*)/,function (...str) {
return str[1]+str[2].padStart(3, "0");
});