I have the following datetime string 2020-5-1 1:2 I used the pattern (\W)(\d{1}) to match any digit with length 1 i.e non zero leaded, 5,1,1,2. This demo shows that pattern succeeded to catch them in the group 2 for every match.
Using Javascript's String replace method, I have tried to turn the datetime sample string to be 2020-05-01 01:02. In this jsbin that runs the following snippet:
var txt = '2020-5-1 1:2'
var output = [];
output[0] = txt.replace(/(\W)(\d{1})/gi,'0$1');
output[1] = txt.replace(/(\W)(\d{1})/gi,'0$2');
console.log(output);
// The output: ["20200-0-0 0:", "202005010102"]
In the first output's entry, it does unexpected behavior, instead of adding 0 to the match, it replaced it with 0! How could I solve this issue?