Escape new lines with JS

后端 未结 3 1610
情歌与酒
情歌与酒 2021-02-13 23:49

I have a string that looks something like this:

\"Line 1\\nLine 2\"

When I call length on it, though, it\'s one character short:



        
3条回答
  •  北海茫月
    2021-02-14 00:20

    In JavaScript, a backslash in a string literal is the start of an escape code, for instance backslash n for a newline. But what if you want an actual backslash in the resulting string? One of the escape codes is backslash backslash, for an actual backslash.

提交回复
热议问题