简体   繁体   中英

JavaScript Converting \n to \r\n

I am having some trouble converting simple \\n to \\r\\n in JavaScript

var text = "Test1\nTest2\nTest3";

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

Does anyone see what's wrong with the above JS? I am trying to do a global replace of \\n with \\r\\n.

The above code does not do the conversion and leaves the \\ns

试试这个regExp:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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