0

Okey, I need some help with a code in Javascript.

If this is the input of a string: "\nLorem ipsum\nMid magnis\nTristique mauris proin"

I want this as output: "\n\t\t\tLorem ipsum\n\t\t\tMid magnis\n\t\t\tTristique mauris proin"

So I tried to do this function:

var text = function (texts) {
    var str = texts;
    return str.replace("\n", "\n\t\t\t");
}
------> "\n\t\t\tLorem ipsum\nMid magnis\nTristique mauris proin"

And it takes only the first "\n" and replace it with "\n\t\t\t".

So please, can I have som help? Cheers!

Tushar
  • 82,599
  • 19
  • 151
  • 169
Z.craze
  • 9
  • 1
  • 2

1 Answers1

4

Use a regex with the global flag:

"\nLorem ipsum\nMid magnis\nTristique mauris proin".replace(/\n/g, '\n\t\t\t');
CD..
  • 68,981
  • 24
  • 147
  • 156