File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes character entities - new line Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "character entities - new line" Watch "character entities - new line" New topic
Author

character entities - new line

Gayatri Sinha
Ranch Hand

Joined: Feb 13, 2006
Posts: 34
All I am trying to do is insert new lines instead of the <br />
That string in dose is what I am getting from the backend and in the front I want new lines instead.

The code below just gives me spaces. Can anyone please help me with this? I dont understand what I am doing wrong.

<html>
<body>

<script type="text/javascript">

var dose = "& lt;br /> ;Here& lt;br /& gt;is a new& lt;br />line& lt;br /& gt;for you";

dose = dose.replace(/& lt;br \/& gt;/g,"& #010;& #013;");
document.write(dose);
</script>
</body>
</html>

The output is "Here is a new line for you"

Please note - I have added the space after & on purpose else the character entiry would be replaced

Thanks

[ April 24, 2007: Message edited by: Gayatri Sinha ]
[ April 24, 2007: Message edited by: Gayatri Sinha ]
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
It's been a long time ... so I'm just copy-posting something I did years ago ... that just looks to me like it's related. Maybe it will give you a hint about something to try. I would have to go back and review regular expressions again to be any more helpful.


re= new RegExp("<br>|<br/>","gi");
article=article.replace(re,"\r\n");
[ April 24, 2007: Message edited by: Roger F. Gay ]

Correlation does not prove causality.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Be aware that when you write the string containing the new lines, they will be ignored as invisible whitespace. So what's the point of the exercise?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Gayatri Sinha
Ranch Hand

Joined: Feb 13, 2006
Posts: 34
Yes, the code you have given is exactly what the code I have written above is trying to do..
But after replacing it with \r\n there is no new line, its just a space instead..

Surprisingly, this works -
var dose = "<br />Here<br />is a new<br />line<br />for you";
dose = dose.replace("<br />","\r\n");
document.write(dose);

But if I have & lt; & gt; (which is what I want) it doesn't (please remove the space after &)
Gayatri Sinha
Ranch Hand

Joined: Feb 13, 2006
Posts: 34
Originally posted by Bear Bibeault:
Be aware that when you write the string containing the new lines, they will be ignored as invisible whitespace. So what's the point of the exercise?


why is the new line ignored?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61437
    
  67

Originally posted by Gayatri Sinha:
why is the new line ignored?


Because that's the way HTML works. Whitespace is not literally interpreted.

If you put the following in your HTML:



It will all appear on one "line". Whitespace is not that important, and newline has no semantics.

And...

Surprisingly, this works -


Actually, your code example does not work. if you put an alert after the call to replace, you will see that all but the first <br/> tags remain in your string.
[ April 24, 2007: Message edited by: Bear Bibeault ]
Roger F. Gay
Ranch Hand

Joined: Feb 16, 2007
Posts: 396
Sorry Gayatri. I guess I was distracted by the detailed. Check the ASCII table at

http://www.asciitable.com/

and you'll find html value / codes. You'll notice that Dec. 10 and 12 are not html values. html codes start at 32, and do not correspond to the values used in text files.

There is no \r\n in html.
[ April 24, 2007: Message edited by: Roger F. Gay ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: character entities - new line