I have a task that has a few rules. Basically, I am separating a number into groups of 3. If the last group only has 1 digit in it, then the last 2 groups have 2 digits instead of 1 group of 3 and 1 group of 1.
For example:
123456789 = 123-456-789
12345678901 = 123-456-789-01
1234567 = 123-45-67
My regex so far has been:
serial.match(/\d{3}|\d+/g)
where serial is a string passed in via function
function format(serial) {
return serial.replace('/-|/s/g', '').match(/\d{3}|\d+/g).join('-');
}
This gets me the majority of the way there, just doesn't split properly when the last match is {4}.
My progress via fiddle: https://jsfiddle.net/dboots/2mu0yp2b/2/