Solved

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

Posted on 2014-11-25
14
125 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

PROBLEM: The other day I was working on adding an ajax request to a webpage that already had a dialog box on the page.  The dialog box was using relative positioning to be positioned next to a form field I had on the page.  Everything was working…
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
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…

792 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