View Full Version : trouble replacing \n with <br>
erank99
11-04-2002, 11:13 PM
When I try to replace the \n from a multi-line textfield it leaves in the line feed and puts in the <br>. I just wan it to replace the \n with the <br>
WhereAt = theText.indexOf("\n");
// Run this do while loop if it is more than -1
do {
theText = theText.replace('\n', '<br>');
WhereAt = theText.indexOf("\n");
} while (WhereAt >-1);
boywonder
11-05-2002, 12:09 AM
This should replace all \n with <br> in any given string:
(where str is your string)
str = str.replace(/\n/g,'<br>');
whammy
11-05-2002, 12:32 AM
Although if you don't want to live in the dark ages of HTML, you might want to consider using XHTML:
(where str is your string)
str = str.replace(/\n/g,'<br />');
http://www.w3schools.com/xhtml
erank99
11-05-2002, 05:26 PM
For some reason it is not replacing it right. It seems to be leaving in the line feed.
WhereAt = theText.indexOf("\n/g");
// Run this do while loop if it is more than -1
do {
theText = theText.replace('\n/g', '<br>');
WhereAt = theText.indexOf("\n/g");
} while (WhereAt >-1);
When I pull it the value of theText in the text area again it looks like this.
"... the end of the paragraph.
<br>The begining of the new paragraph..."
Any ideas.
beetle
11-05-2002, 05:56 PM
You need to replace the CRs (Macintosh), the LFs (Unix), and the CRLFs (Windows)
theText = theText.replace(/(?:\n\r|\n|\r)/g, '<br />');
Note: erank99, don't encapsulate the pattern as a string, that will cause it to not work...leave it just how it's typed. Both boywonder and whammy typed it correctly.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.