Javascipt: how to use nl2br function
March 3, 2011 | In: javascript, Regular Expression, web development
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, "<br />"); } |
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, "<br />"); } |
Here we used regular expression ‘/g’. That’s it.