0

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.

7
  • You mentioned before the data is entered in the database, you tried preg_replace and nl2br to add <br />. Are you saying that the raw data also did not save with the <br />? Is there a way to check the raw data like by using phpMyAdmin to confirm... Here's a quick debug tip: try another similar procedure and see if it also fails. Like try replace all spaces (\s) and see if the replace method works at least. It may give you some ideas of what to check. Commented Feb 27, 2014 at 23:07
  • One other tip: You don't need to add <br> to get line breaks. Just use white-space:pre-wrap; in your css, and leave the text alone. Or use pre-line if you want to have multiple spaces collapsed into one. See developer.mozilla.org/en-US/docs/Web/CSS/white-space . Commented Feb 27, 2014 at 23:30
  • Can you provide an example of what exactly d.body_text looks like? Are you sure you have newlines and not \ns as two characters? Commented Feb 27, 2014 at 23:35
  • @muistooshort you are right. they are \n chars in the database. e.g hey guys\nhow are you?\n Commented Feb 28, 2014 at 0:09
  • So d.body_text.replace(/\\n/g, '<br>') gives you the results you want then? Commented Feb 28, 2014 at 0:15

1 Answer 1

2

Based on the comments, it seems that someone has converted real newline characters (i.e. "\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)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.