This Perl script is what I want to implement in JavaScript (source):
s/([0-9]+)/sprintf('%04d',$1)/ge;
Obviously sprintf is not available in JavaScript, but I know you can build a function that work directly on the number of concern, however in my case I have a long string containing possibly multiple numbers to convert string such as this: abc8 23 123
into this: abc000008 000023 000123
(using 6 digits as an example).
So it's either some clever regex that I don't get, or somehow find some way to split the long string into an array of distinct string and numbers and only act on the number one by one.
I really want to avoid the latter approach because 1) Speed is an issue in this case, and 2) The regex involved in splitting strings at the right place seems harder than adding leading zeros at this point.
In the end, is there any other way to go about it with JavaScript's regex?