[英]How to replace \n with < br > in escaped string?
I know similar questions have been asked before. 我知道以前也曾问过类似的问题。 In all the ones I am aware of I did not find a solution, and I have already tried several things on my own which I do not list for the sake of brevity.
在我所知道的所有方面,我都没有找到解决方案,并且我已经尝试过一些事情,为简洁起见,我没有列出。
In Angular I am trying to replace \\n
with <br>
in an escaped string which looks like ABC\\nDEF\\n\\nGHI
. 在角我试图替换
\\n
与<br>
一个转义字符串,它看起来像ABC\\nDEF\\n\\nGHI
。
I am using var replaced = original.replace(/\\r?\\n|\\r/g, "<br>")
which works fine if original
has not been escaped. 我正在使用
var replaced = original.replace(/\\r?\\n|\\r/g, "<br>")
,如果original
未转义,则可以正常工作。
How can I obtain the same when original
has been escaped? original
被转义后如何获得相同的文件?
Since your string is escaped, you need to match literal backslashes. 由于字符串已转义,因此需要匹配文字反斜杠。
Here is how you can match the linebreaks now: 现在,您可以通过以下方式匹配换行符:
var replaced = original.replace(/(?:\\r\\n|\\r|\\n)/g, "<br>");
In a literal notation, a double backslash matches a literal backslash. 在文字符号中,双反斜杠与文字反斜杠匹配。
I might be misunderstanding the question, but wouldn't 我可能会误解这个问题,但不会
var result = escaped.replace(/(\n)/g, '<br>');
Do the trick? 骗人吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.