在Ionic 3中将\ r \ n字符转换为新行 [英] Convert \r\n characters to new line in Ionic 3
问题描述
我希望能够将字符串中的\ r \ n个字符转换为新行
I want to be able to convert \r\n characters in a string to new lines
示例 约翰现在正在等待.\ r \ n我们能告诉您有关他的情况吗? \ r \ n他是一个伟大的人
Example John is waiting now.\r\nCan we tell you about him. \r\nHe is a great person
转换为
约翰现在正在等待.
我们能告诉你关于他的事吗?
Can we tell you about him.
他是一个伟大的人
尝试过但无济于事
nl2br(text: string) {
return text.replace(new RegExp('\r?\n','g'), '<br />');
}
推荐答案
我认为问题可能是由于您的\r\n
字符因为过度转义"而可见的.
I think the problem might be due to the fact that your \r\n
characters might be visible because they are "over escaped".
此外,我不知道要执行替换的确切上下文,但我认为您不必用<br/>
Furthermore I don't know exactly the context your replace has to be performed but I don't think you have to replace them by a <br/>
因此,您可以做一些愚蠢的事情
You could thus do something silly like
nl2br(text: string) {
return text.replace('\\r\\n', '\n');
//or return text.replace('\\r\\n', '<br/>') if it is really what you need
}
还请注意,如果您同时使用了转义的\n
和\r
,则可以
Note also that if you have a mix of escaped \n
and \r
, you can
nl2br(text: string) {
return text.replace(/\\r\\n|\\r|\\n/gi, '\n');
//or ...'<br/>');
}
这篇关于在Ionic 3中将\ r \ n字符转换为新行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!