Solved

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

Posted on 2014-11-25
14
121 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
Comment Utility
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
Comment Utility
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
Comment Utility
I am printing in plain HTML table not in text area
0
 
LVL 9

Accepted Solution

by:
Brian Tao earned 500 total points
Comment Utility
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
Comment Utility
By the way, @Jargonics, str.replace("<br>", "\n") only replaces the first occurrence.
0
 
LVL 20

Author Comment

by:Sathish David Kumar N
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 20

Author Comment

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

Author Comment

by:Sathish David Kumar N
Comment Utility
0
 
LVL 9

Expert Comment

by:Brian Tao
Comment Utility
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
Comment Utility
@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
Comment Utility
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
Comment Utility
escapeHTML not allow me to that ??  is there any way to achive this ??
0
 
LVL 9

Expert Comment

by:Brian Tao
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction A frequently asked question goes something like this:  "I am running a long process in the background and I want to alert my client when the process finishes.  How can I send a message to the browser?"  Unfortunately, the short answer …
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 the basics of jQuery, including how to invoke it on a web page. 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.: (CODE)
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now