Javascipt: how to use nl2br function

In php we have a function to replace nl2br means new line to break format. but when we need this functionality in our javascript code then what we do?
We can do by simply manner:

  function nl2br(value) {
     return value.replace(/\n, "
"); }

Above function will return the converted ‘\n’ to ‘
‘. But above function has a disadvantage. It convert only first line of the value. We can convert all new lines to break format with below function:

  function nl2br(value) {
     return value.replace(/\n/g, "
"); }

Here we used regular expression ‘/g’. That’s it.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

*