I'm trying to show a text that i'm getting from the server with html tags.
Let's say i got
"a\nb\nc\nd\ne\nf\n"
and i want to show
a
b
c
d
e
f
i tried using jquery text() but i get empty string:
var answer = params.question.answer;
$('#answer_text').html($(answer).text());
i also tried with regex but nothing happens:
var regex = /(<([^>]+)>)/ig;
var answer = params.question.answer.replace(regex, '');
$('#answer_text').html(answer);
You need to convert \\n
into <br/>
for creating line breaks in html:
var answer= "a\nb\nc\nd\ne\nf\n";
$('#answer_text').html(answer.replace(/\n/g, "<br />"));
Using REGEX Remove \\n
and add <br />
tag.
Try:
var answer = "a\nb\nc\nd\ne\nf\n";
var regex = /\n/gi;
$('#answer_text').html(answer.replace(regex, "<br />"));
Another option is to use the white-space rule
var answer = "a\\nb\\nc\\nd\\ne\\nf\\n"; $('#answer_text').html(answer);
#answer_text { white-space: pre-line; }
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <div id="answer_text"></div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.