Let's say I have a line:
"__3_"
... which I would like to refer to:
"__###_"
basically replacing the integer with duplicate occurrences of # , equivalent to an integer value. How can I achieve this?
I understand that backlinks can be used with str.replace()
var str = '__3_' str.replace(/[0-9]/g, 'x$1x')) > '__x3x_'
And we can use str.repeat(n) to repeat sequences of strings n times.
But how can I use the backreference from .replace() as an argument to .repeat() ? For example, this does not work:
str.replace(/([0-9])/g,"#".repeat("$1"))
javascript regex
Reuben L.
source share