I don't understand why this is happening, basically i got some description of some app from Google play store, in the form of JSON, now i am using jQuery to add the description into some div, but instead of actually printing as it is, it gives a block of paragraph and ignores \n or <br /> tag.
var description = json.description;
alert(description);
When I alert this description, as shown above, there is proper paragraph format with next line[\n] included just like I want it, but when I add it to the required div with either html() or text(), it ignores the next line \n
$('#appName').html(description);
I tried to replace the next line with the
tags, but still the result was same, I don't know why
var test = description.replace(/\s\n+/g, '<br />');
$('#appName').html(description)
For anyone who is searching for a css solution. It's not neccecarry to do this with a regex as you can simply add the white-space property. If you set it to pre-wrap, \n results in a new line.
.show-white-space {
white-space: pre-wrap;
}
just add the class to your <p> or <div> tag.
https://developer.mozilla.org/de/docs/Web/CSS/white-space
Try this
var test = description.replace(/\n/g, '<br />');
$('#appName').html(test );
Your regex would not work if there is no white space before newline character. also you dont need to put + as /g will replace all new line characters.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With