Remove Backslashes from Json Data in JavaScript

前端 未结 5 976
误落风尘
误落风尘 2020-12-08 20:53

Remove Backslashes from JSON Data in JavaScript or jQuery

var str = \"{\"data\":\"{\\n \\\"taskNames\\\" : [\\n \\\"01 Jan\\\",\\n \\\"02 Jan\\\",\\n \\\"03          


        
5条回答
  •  执笔经年
    2020-12-08 21:46

    Your string is invalid, but assuming it was valid, you'd have to do:

    var finalData = str.replace(/\\/g, "");
    

    When you want to replace all the occurences with .replace, the first parameter must be a regex, if you supply a string, only the first occurrence will be replaced, that's why your replace wouldn't work.

    Cheers

提交回复
热议问题