1

I'm getting the following raw string from a JSON file. I console.log it display fine.

"Hello, my name is Jane, from "IStaff" company.\r\n\r\n.We are interested in your service.\r\n\r\n.Please call me back "

When I rendering this string, there is no new lines.

Yerlan Yeszhanov
  • 2,149
  • 12
  • 37
  • 67
  • How exactly are you trying to insert it into the `
    `? The code matters
    – CertainPerformance Mar 21 '19 at 05:51
  • New lines won't display in the browser. You need to use `
    ` tag for a new line on the browser. Use [nl2br](http://php.net/manual/en/function.nl2br.php) to parse your JSON and convert all new lines to break tags before sending to client side.
    – nice_dev Mar 21 '19 at 05:52

5 Answers5

7

the best way in my case is just add in css : white-space: pre-wrap;

Yerlan Yeszhanov
  • 2,149
  • 12
  • 37
  • 67
1

If you insert that string into HTML, it will render by literally showing the \n and \r elements:

const string = 'Hello, my name is Jane, from "IStaff" company.\r\n\r\n.We are interested in your service.\r\n\r\n.Please call me back';
document.write(string);

You need to replace them with HTML <br> elements:

const string = 'Hello, my name is Jane, from "IStaff" company.\r\n\r\n.We are interested in your service.\r\n\r\n.Please call me back';
const renderString = string.replace(/(?:\r\n|\r|\n)/g, "<br>");
document.write(renderString);

Regex above from this answer.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
1

I think this problem occurs when you use innerHTML, use <br/> instead of /r/n.

Example:

"Hello, my name is Jane, from 'IStaff' company.<br/> <br/> .We are interested in your service.<br/> <br/> .Please call me back "

"Hello, my name is Jane, from 'IStaff' company.

.We are interested in your service.

.Please call me back "

Madhusudana
  • 302
  • 3
  • 12
0

Whitespace characters like newlines do not render in HTML. You have to change the \n into <br>

function nl2br(str, is_xhtml) {
  if (typeof str === 'undefined' || str === null) {
    return '';
  }
  var breakTag = (is_xhtml || typeof is_xhtml === 'undefined') ? '<br />' : '<br>';
  return (str + '').replace(/([^>\r\n]?)(\r\n|\n\r|\r|\n)/g, '$1' + breakTag + '$2');
}

dataReceveid = function(json) {
  $("div").html(nl2br(json.str));
}

$(function() {
  dataReceveid({
    str: '"Hello, my name is Jane, from "IStaff" company.\r\n\r\n.We are interested in your service.\r\n\r\n.Please call me back "'
  });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div></div>
Soviut
  • 88,194
  • 49
  • 192
  • 260
mastermind
  • 1,057
  • 1
  • 8
  • 15
0

The browser doesn't know what does it means \r\n. The language the web browser know only is HTML and \r\n is not an HTML language word. To break the line in the browser you have to use HTML language tag <br /> so the browser can consider it and break the lines.

So the simple solution to your problem is replacing \r\n with <br /> HTML tag/element.

Syed Aqeel
  • 1,009
  • 2
  • 13
  • 36