0

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. I wonder if there's any easy way to convert s0 and s1 both way. I understand I could do replace. However, there might be a whole lot of other escape sequences to take care of.

1
  • 2
    JSON.parse(s0)? Commented Dec 15, 2018 at 21:28

1 Answer 1

3

You could use JSON methods:

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))

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.