简体   繁体   English

JavaScript将\\ n转换为\\ r \\ n

[英]JavaScript Converting \n to \r\n

I am having some trouble converting simple \\n to \\r\\n in JavaScript 我在JavaScript中将简单的\\ n转换为\\ r \\ n时遇到麻烦

var text = "Test1\nTest2\nTest3";

 var converted = text.replace("/\\n/g", "\r\n");

Does anyone see what's wrong with the above JS? 有人看到上述JS有什么问题吗? I am trying to do a global replace of \\n with \\r\\n. 我正在尝试用\\ r \\ n全局替换\\ n。

The above code does not do the conversion and leaves the \\ns 上面的代码不执行转换,而是保留\\ ns

试试这个regExp:

var converted = text.replace(/(\r)?\n/g, "\r\n");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM