Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace an HTML <br> with newline character "\n"?

How can I replace HTML <BR> <BR/> or <BR /> with new line character "\n"

like image 739
Anoop Avatar asked Nov 09 '11 08:11

Anoop


People also ask

Is BR and \n same?

The <br> or <br /> tag is an HTML element that will display everything after this <br> or <br /> by starting from new line when rendered in browser while the \n is used to jump to next line in the source code or the output prompt in standard output.

How do I replace all line breaks in a string with br /> elements?

The RegEx is used with the replace() method to replace all the line breaks in string with <br>. The pattern /(\r\n|\r|\n)/ checks for line breaks. The pattern /g checks across all the string occurrences.

How to replace new line with new line character in HTML?

Upon click of a button, we will replace the new line with new line character ( ) and display the result on the screen. Please have a look over the code example and the steps given below. We have 3 elements in the HTML file ( div, button, and h1 ). The div element is just a wrapper for the rest of the elements.

How to replace newline characters in an ArrayList using JavaScript?

What the method does is to simply iterate through the lines in the input and add it to an ArrayList. After iterating through all of the lines (thus having split the input), the list elements are joined together with the <br />break line tag, effectively replacing the newline characters.

How to replace newline( ) with an HTML break tag?

In today’s post, we will learn both the functions to replace newline ( ) with an HTML break tag ( <br/> ). The replaceAll () is an in-built method provided by JavaScript which takes the two input parameters and returns a new String in which all matches of a pattern are replaced with a replacement.

How do I replace a line in a string?

Replace (‘string’, ‘ ’, ”) will replace only the substring in the whole string. If the substring is not found, it’ll not replace anything and the flow will continue. To replace a new line you must use the right ‘new line’ character in your flow.


2 Answers

You're looking for an equivilent of PHP's nl2br(). This should do the job:

function br2nl(str) {     return str.replace(/<br\s*\/?>/mg,"\n"); } 
like image 185
Polynomial Avatar answered Sep 21 '22 09:09

Polynomial


A cheap function:

function brToNewLine(str) {     return str.replace(/<br ?\/?>/g, "\n"); } 

es.

vat str = "Hello<br \>world!"; var result = brToNewLine(str); 

The result is: "Hello/nworld!"

like image 23
Prais Avatar answered Sep 23 '22 09:09

Prais