how to create a regex that automatically adds a space after every 3-4 letters?
I currently have a regex created, but it will add a space only after adding a letter to the input:
function format(s) {
return s.toString().replace(/\d{3,4}?(?=..)/g, "$& ");
}
console.log(format(1234567890));
console.log(format(123456789));
console.log(format(1234567));
console.log(format(123456));
console.log(format(1234));
console.log(format(123));
So the output is like this.
123 456 7890
123 456 789
123 4567
123 456
1234
123
How to rebuild this regex so that the output is like this?
123 456 789 0
123 456 789
123 456 7
123 456
123 4
123
Thank you.