I want to replace empty lines in my string with an iterating number
e.g.
replace
String:
"My first line
My second line
My third line"
with
"
1
My first line
2
My second line
3
My third line"
I can match and replace these lines using
var newstring = TestVar.replace (/(^|\n\n)/g, "\nhello\n");
However I’m struggling to add a function that will add an iterating number to each one.
Can you help?
TIA,
Gids
Yes, you can do that in javascript. You just need to pass a function as a second argument to
replace.function actually get a lot of parameters based on which you can decide what value you want to replace with but we don’t need any of them for this task.
However, it is good to know about them, MDC has a great documentation on the topic