• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

character entities - new line

 
Gayatri Sinha
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • 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 ]
 
Roger F. Gay
Ranch Hand
Posts: 408
  • Mark post as helpful
  • send pies
  • 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 ]
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
Author and ninkuma
Marshal
Pie
Posts: 64994
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • 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: 408
  • Mark post as helpful
  • send pies
  • 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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic