So I have this block of jquery inside a coffeescript file;
$("#parser-results").append(
"<tr><td>" + d.id + "</td>" +
"<td>" + d.received_at + "</td>" +
"<td>" + d.from_address + "</td>" +
"<td>" + d.to_address + "</td>" +
"<td>" + d.subject_text + "</td></tr>" +
"<tr><td colspan=5><div class='body-text'>" + d.body_text.replace(/\n/g, "<br />") + "</div></td></tr>"
)
The problem here relates to the variable d.body_text
, which is a retrieved TEXT
field from a database.
I am trying to replace all instances of \\n
with a <br>
and I have literally tried every jquery, javascript and coffeescript variation of every approach that I could find on the internet. I've also tried preg_replace
and nl2br
before the data is entered in the database to no avail.
Also, the data is coming from a mail parsing service called mailparser.io. I doubt it has anything to do with that but I'm really just not sure anymore.
What is missing here? I need some help identifying what the problem could be.
Based on the comments, it seems that someone has converted real newline characters (ie "\\n"
in (Coffee|Java)Script) to the two character string '\\\\' + 'n'
in your database. That would explain why:
d.body_text.replace(/\n/g, "<br />")
does nothing useful: there are no newlines to replace with <br>
s so, well, nothing gets replaced. The solution is to replace what's really there:
d.body_text.replace(/\\n/g, "<br />")
// ------------------^^^ This gives you a literal \n (as two characters)
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.