简体   繁体   中英

How to Convert text with \n and add real new line without character in node js?

I have text response like this, I want to remove special character \n and make it to be real new line.

The first response with special character

-----BEGIN MESSAGE-----\n v1.60\n\nhQEMAwPgeMJUXSL5Bps+U3lC8tnc\nD8s2Aeb7UtryIRABy/U2PCENdNKH5Jm+CI1CO\n/BBlzx77Z4YGNVfh2G+d62\nln0OjxCcTHbbTM/1XjNYqRTzWZInIVAvGzKgw/V+8f2b0NM4M9kPJRy65uov42Iv\n125RWFv/uaCtgY2XHzk=\n=Xyt6\n-----END MESSAGE-----\n

Actually I want to make the response to be like this,

-----BEGIN MESSAGE-----

By+ZEGhZCB8kWnIxOlgFrvABtc3n5AzHteDk9WJ4vIrr52T5tTlQntAFpm6P
8Ars0HETgvE7klIxFSrSXy4D350gn3TwJywhR3Go0WxujNRhi8MhXMRYmg/o
8Ars0HETgvE7klIxFSrSXy4D350gn3TwJywhR3Go0WxujNRhi8MhXMRYmg/o
8Ars0HETgvE7klIxFSrSXy4D350gn3TwJywhR3Go0WxujNRhi8MhXMRYmg/o
8Ars0HETgvE7klIxFSrSXy4D350gn3TwJywhR3Go0WxujNRhi8MhXMRYmg/o
8Ars0HETgvE7klIxFSrSXy4D350gn3TwJywhR3Go0WxujNRhi8MhXMRYmg/o
=SEl/

-----END PGP MESSAGE-----

Those are two different strings but I use that as example.

No special characters, how to achieve convert the response to be like the second response in node.js.

Like this? No need for any processing

In Node

 const str = `-----BEGIN MESSAGE-----\n v1.60\n\nhQEMAwPgeMJUXSL5Bps+U3lC8tnc\nD8s2Aeb7UtryIRABy/U2PCENdNKH5Jm+CI1CO\n/BBlzx77Z4YGNVfh2G+d62\nln0OjxCcTHbbTM/1XjNYqRTzWZInIVAvGzKgw/V+8f2b0NM4M9kPJRy65uov42Iv\n125RWFv/uaCtgY2XHzk=\n=Xyt6\n-----END MESSAGE-----\n` console.log(str)

If escaped

 const str = `-----BEGIN MESSAGE-----\\n v1.60\\n\\nhQEMAwPgeMJUXSL5Bps+U3lC8tnc\\nD8s2Aeb7UtryIRABy/U2PCENdNKH5Jm+CI1CO\\n/BBlzx77Z4YGNVfh2G+d62\\nln0OjxCcTHbbTM/1XjNYqRTzWZInIVAvGzKgw/V+8f2b0NM4M9kPJRy65uov42Iv\\n125RWFv/uaCtgY2XHzk=\\n=Xyt6\n-----END MESSAGE-----\\n` console.log(str.replace(/\\n/g,"\n"))

Assuming that your string contains the escape sequence \n and that you aren't just talking about the JavaScript source code :

// I've escaped the \ so that the string contains \n instead of an actual new line
const myString = "This is a string\\nwith a slash n in it";

then you can do a simple replacement:

const unEscaped = myString.replace(/\\n/g, "\n");
process.stdout.write(unEscaped);

Although, if you have other JS sequences in the data then you might be better off treating it as JSON:

const parsed = JSON.parse(myString);
process.stdout.write(parsed);

If you are talking about the source code, then the problem will be with how you are inspecting the string and not with the string itself. You didn't say what you were doing, but if I were to take a guess I'd say "Use process.stdout.write() instead of console.log() .

I think your operating system is Windows.

in windows for new line you must use \r\n instead of just \n.

but i think \n is fine and you must try opening your file in a editor that support non-Windows newlines (eg Wordpad).

if you want to replace it try:

str.replace(/\\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