0

I'm trying to load a text file to display in a pre tag using jQuery. I would like to display all the text including the escape sequences like new line, tab and so on. If I use jQuery.get('log.cpp', function(data) { ... and I try to display the text with:

var str = data.replace(/(?:\r\n|\r|\n)/g, '<br>');
$('#fileName').html(str);

Then many characters s like <> and the text surrounded by those chars are not displayed at all. Since I try to load and display some source code, a line with #include <header> will be presented as #include only. If i try to use the following:

var str = data.replace(/(?:\r\n|\r|\n)/g, '\n');
$('#fileName').text(str);

No tabs or new lines are presented on screen and the text is shown as one line only.

Any hint? Thank you very much

Rory McCrossan
  • 306,214
  • 37
  • 269
  • 303
AmiStack
  • 23
  • 6
  • Get rid of the `replace()` call and just use `text()`. Then the code will work fine. https://jsfiddle.net/RoryMcCrossan/vsx8tucj/ – Rory McCrossan Jul 10 '19 at 13:17
  • Thank you very much for your suggestion, that lead me to find the error: the dynamically created html tag was "prev" instead of "pre". – AmiStack Jul 10 '19 at 13:27

1 Answers1

0

Just add this where you want to read or display it

style="white-space: unset;"

When you want to read

<textarea type="text" style="white-space: unset;"></textarea>

When you want to display

<pre style="white-space: unset;">Your text with line breaks</pre>
Shervin Ivari
  • 828
  • 3
  • 9
  • 20