Replacing newline character in javascript

回眸只為那壹抹淺笑 提交于 2019-12-03 06:07:56
Felipe

Try this:

myString = myString.replace(/[\r\n]/g, "<br />");

Update: As told by Pointy on the comment below, this would replace a squence of \r\n with two <br />, the correct regex should be:

myString = myString.replace(/\r?\n/g, "<br />");

try replace(/\r\n|\n/, '<br />')

This worked for me:

str = str.replace(/\\n|\\r\\n|\\r/g, '<br/>');

Using double slash

CSS:

 white-space: pre-wrap;

Is a far more eficient method.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!