Solved

<br> replace \n (newline) is not working

Posted on 2014-11-25
14
124 Views
Last Modified: 2014-11-26
Hi,

<sathish><br>Kumar<br> this is my text,

when i do str.replace(/<br \/>/gi,'\n');
                             or
                 str.replace(/<br>/gi,"\n");

both are not working ?? why ? can you give me some suggestion
0
Comment
  • 7
  • 6
14 Comments
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40466120
Well, the second one works for me.  Please see http://jsfiddle.net/9oc8pjet/
Maybe your str isn't what you think it is?  You may want to post your script here so that someone can find the problem.
0
 
LVL 1

Expert Comment

by:Jargonics
ID: 40466135
If html tags are part of the string, why won't you use str.replace("<br>", "\n"); ?
Both won't work where? In an HTML page or in a text-area? Depends what result you are expecting and where you want to see it.
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40466138
I am printing in plain HTML table not in text area
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 9

Accepted Solution

by:
Brian Tao earned 500 total points
ID: 40466155
It also works in HTML table.  Please see http://jsfiddle.net/9oc8pjet/3/.
So why don't you just post your script here so that people can help you?
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40466160
By the way, @Jargonics, str.replace("<br>", "\n") only replaces the first occurrence.
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40466164
function openNewWindow1(str)
{
loadProgressWindow(500, 250,test)

	alert(escapeHtml(str));
	var newMsg = '';
	
	newMsg += '<table border="0" cellpadding="0" cellspacing="0" width="400">\n';
	newMsg += '<tr>\n';
	newMsg += '	</tr>\n';
	newMsg += '<tr>\n';
	newMsg += '		<td>&nbsp;</td>\n';
	newMsg += '	</tr>\n';
	newMsg += '	<tr>\n';
	newMsg += '		<td align="center">\n';
	newMsg += '			<table border="1" cellpadding="2" cellspacing="0" width="90%" align="center">\n';
	newMsg += '				<tr>\n';
	newMsg += '					<td><br>'+escapeHtml(str)+'<br></td>\n';
	newMsg += '				</tr>\n';
	newMsg += '			</table>\n';
	newMsg += '		</td>\n';
	newMsg += '	</tr>\n';
	newMsg += '	<tr>\n';
	newMsg += '		<td></td>\n';
	newMsg += '	</tr>\n';
	newMsg += '<tr>\n';
	newMsg += '		<td>&nbsp;</td>\n';
	newMsg += '	</tr>\n';
	newMsg += '	<tr>\n';
	newMsg += '		<td align="center">\n';
	newMsg += '			<input type="button" name="button" value="Close" onclick="closeNewWindow()"/>\n';
	newMsg += '		</td>\n';
	newMsg += '	</tr>\n';
	newMsg += '</table>\n';
	
}

Open in new window

0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40466168
function escapeHtml(unsafe) {
	
	if (typeof unsafe  == "undefined"){
		return unsafe;
	}else
		{

    return unsafe
         .replace(/<br>/g,"\n")
         .replace(/&/g, "&amp;")
         .replace(/</g, "&lt;")
         .replace(/>/g, "&gt;")
         .replace(/"/g, "&quot;")
         .replace(/'/g, "&#039;");
       
         
		}
}

Open in new window

0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40466269
output displyed in <div> tag
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40466275
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40466277
It still works using your functions (with some minor modifications in openNewWindow1 to make it work as you didn't post everything, but didn't change anything in escapeHtml).  Please see http://jsfiddle.net/261kun30/

I still suspect that your str is not <sathish><br>Kumar<br>
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40466324
@taoyipai thanks. but <br> is replaced with new line but sathish kumar are in same line not  the 2 line

the output should
sathish
kumar

Open in new window


I still suspect that your str is not <sathish><br>Kumar<br>//Yes correct some something like the same manaer
0
 
LVL 9

Assisted Solution

by:Brian Tao
Brian Tao earned 500 total points
ID: 40466341
If you want to display 2 lines on an HTML page, you need a <br> tag.  The newline character is treated just as a space by the browser.
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
ID: 40466401
escapeHTML not allow me to that ??  is there any way to achive this ??
0
 
LVL 9

Expert Comment

by:Brian Tao
ID: 40466548
No, it has nothing to do with your escapeHtml function.  It's the W3C standard.  Any number of consecutive whitespace characters (including TAB, newline, space and others) would be displayed as a single space, unless you specifically tell it not to, for example, wrap the text with a pair of <pre></pre> tags.
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Uncaught Type error cannot assign to read only property 6 27
How do you setTimeout in JQuery? 1 22
where is team.js? 3 25
How to show span when clicked on? 10 21
OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

773 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question