javascript:替换换行符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2225514/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
javascript: replace linebreak
提问by Fuxi
I'm having a string which contains a chr(13) as linebreak. How can i replace it with eg. <br>? I tried mystring.replace("\n","<br>");but it didn't work
我有一个包含 chr(13) 作为换行符的字符串。我怎样才能用例如替换它。<br>? 我试过了,mystring.replace("\n","<br>");但没有用
Thanks in advance.
提前致谢。
回答by Mark Byers
"\n"is chr(10). I think you want "\r":
"\n"是 chr(10)。我想你想要"\r":
mystring.replace("\r", "<br>");
Updated: To replace ALL \r use a regular expression:
更新:要替换 ALL \r 使用正则表达式:
mystring.replace(/\r/g, "<br>");
If you want it to work with Windows, Unix and Mac style line breaks use this:
如果您希望它与 Windows、Unix 和 Mac 样式的换行符一起使用,请使用以下命令:
mystring.replace(/\r?\n|\r/g, "<br>");
回答by Mic
theString.replace(/\n|\r/g, '<br />')

