say a I have a var like
var str = 'the @ brown @ fox @ jumped @ over @ the @ fence'
can I replace it with
var str = 'the 1 brown 2 fox 3 jumped 4 over 5 the 6 fence'
maybe array.from
can help
say a I have a var like
var str = 'the @ brown @ fox @ jumped @ over @ the @ fence'
can I replace it with
var str = 'the 1 brown 2 fox 3 jumped 4 over 5 the 6 fence'
maybe array.from
can help
4
Answers
You can use
String#replace
with a callback that keeps track of the count.Or, similarly with
String#replaceAll
:Regex
approach:using replace with
i
which is gonna be incremented each time find character@
:for-loop
approachreduce
approach:You could take a closure with a start value of one for the index.