If you split the space, you can add leading zeros using a simple function, for example:
function addZeros(n) { return (n < 10)? '00' + n : (n < 100)? '0' + n : '' + n; }
So, you can check the length of the string and, if it is less than 6, divide by space, add zeros to the number, and then combine it together.
Or as a regex:
function addZeros(s) { return s.replace(/ (\d$)/,' 00$1').replace(/ (\d\d)$/,' 0$1'); }
I am sure that someone can do this with one replacement, and not with two.
Edit - Examples
alert(addZeros('MR 3')); // MR 003 alert(addZeros('MR 23')); // MR 023 alert(addZeros('MR 123')); // MR 123 alert(addZeros('foo bar 23')); // foo bar 023
At the end of the line will be placed one or two zeros from a number in a row with a space in front of it. He doesn't care what bit in front of space.
RobG Jun 24 2018-11-11T00: 00Z
source share