How to convert a string with escaped characters into a "normal" string

Posted by M28 on Stack Overflow See other posts from Stack Overflow or by M28
Published on 2010-06-03T14:24:00Z Indexed on 2010/06/03 14:34 UTC
Read the original article Hit count: 282

Filed under:
|

Let's say that I have like this: "\\n", I need to convert it to a string like if the interpreter would do it: \n.

A simple replace like this wouldn't work:

function aaa(s){
    return s.replace(/\\n/gm,'\n').replace(/\\\\/gm,'\\');
}

I need this behaviour:

  • "Line 1\nLine 2" => Line 1<Line break>Line 2
  • "Line 1\\nLine 1" => Line 1\nLine1

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about parsing