简体   繁体   English

JavaScript字符串编码/解码

[英]Javascript string encoding/decoding

I have some strings like s0 = "\\"first line,\\\\nsecond line,\\\\n\\"" , and some other strings like s1 = "first line,\\nsecond line,\\n" , where s0 is like the string representation of the whole s1 . 我有一些字符串,例如s0 = "\\"first line,\\\\nsecond line,\\\\n\\"" ,还有一些其他字符串,例如s1 = "first line,\\nsecond line,\\n" ,其中s0就像字符串整个s1表示。 I wonder if there's any easy way to convert s0 and s1 both way. 我想知道是否有任何简单的方法可以同时转换s0s1 I understand I could do replace. 我知道我可以取代。 However, there might be a whole lot of other escape sequences to take care of. 但是,可能还有很多其他转义序列需要注意。

You could use JSON methods: 您可以使用JSON方法:

 s0 = "\\"first line,\\\\nsecond line,\\\\n\\"", s1 = "first line,\\nsecond line,\\n" console.log('Parse s0 ::', JSON.parse(s0)) console.log('Stringify s1 ::', JSON.stringify(s1)) 

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

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