[英]JQuery/Coffeescript unescape variable string
So I have this block of jquery inside a coffeescript file; 所以我在coffeescript文件中有这个jquery块;
$("#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. 这里的问题涉及变量d.body_text
,它是从数据库中检索的TEXT
字段。
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. 我试图用一个<br>
替换\\n
所有实例,并且我已经尝试了每一种我在互联网上可以找到的方法的jquery,javascript和coffeescript变体。 I've also tried preg_replace
and nl2br
before the data is entered in the database to no avail. 在将数据输入数据库之前,我还尝试了preg_replace
和nl2br
,但无济于事。
Also, the data is coming from a mail parsing service called mailparser.io. 此外,数据来自名为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. 根据评论,似乎有人将真实的换行符(即(咖啡| Java)脚本中的"\\n"
)转换为数据库中的两个字符串'\\\\' + 'n'
。 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. 没有任何用处:没有新行可以替换为<br>
s,所以,没有任何东西可以被替换。 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.