How can I turn line breaking into <br /> in Javascript?

Asked

Viewed 3,131 times

12

In PHP, we can convert a line break to a <br/> through function nl2br.

What about Javascript? How can I do this safely?

I decided to ask the question because I don’t know if a simple replace("\n") fits for all cases of line break.

2 answers

14


  • 5

    +1 for the xhtml ;)

  • 3

    I have nothing against the answer having more votes and having been accepted, I think she good, but this XHTML does not convince me. Why do this? If it is not for a human to read, it makes no difference. Choose a pattern and always use it. XHTML is obsolete. I put it in mine the way the AP put it in the question, but I would use my code one <br> which is the default of HTML 5 and which is accepted by all browsers.

10

  • 1

    Exactly! I was worried it would be more complex than a simple .replace("\n", '<br/>'). Thanks :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.