简体   繁体   English

如何使用 \n 转换文本并在 node.js 中添加没有字符的真正新行?

[英]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.我有这样的文本响应,我想删除特殊字符 \n 并使其成为真正的新行。

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.没有特殊字符,如何实现将响应转换为类似于 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 :假设您的字符串包含转义序列\n并且您不只是在谈论 JavaScript源代码

// 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:不过,如果数据中有其他 JS 序列,那么最好将其视为 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() .你没有说你在做什么,但如果我猜测我会说“使用process.stdout.write()而不是console.log()

I think your operating system is Windows.我想你的操作系统是 Windows。

in windows for new line you must use \r\n instead of just \n.在 windows 中换行你必须使用 \r\n 而不仅仅是 \n。

but i think \n is fine and you must try opening your file in a editor that support non-Windows newlines (eg Wordpad).但我认为 \n 很好,您必须尝试在支持非 Windows 换行符(例如写字板)的编辑器中打开您的文件。

if you want to replace it try:如果你想更换它试试:

str.replace(/\\n/g, \r\n);

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

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