?
Solved

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

Posted on 2014-11-25
14
Medium Priority
?
134 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
LVL 9

Accepted Solution

by:
Brian Tao earned 2000 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 2000 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

Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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…
Suggested Courses

764 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