• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

character entities - new line

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Gayatri Sinha
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 409
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic