双引号替换成单引号
let params = {"type":"survey_1","option1":"6","option2":"Yes","option3":"6","option4":"how"}
let str = JSON.stringify(params).replace(/"/g, ‘\‘‘) console.log(str)
{‘type‘:‘survey_1‘,‘option1‘:‘6‘,‘option2‘:‘Yes‘,‘option3‘:‘6‘,‘option4‘:‘how‘}
双引号替换成\"
let params = {"type":"survey_1","option1":"6","option2":"Yes","option3":"6","option4":"sasdasd"} let str = JSON.stringify(params).replace(/"/g, ‘\\"‘)
console.log(str)
结果:{\"type\":\"survey_1\",\"option1\":\"5\",\"option2\":\"No, It took multiple attempts\",\"option3\":\"5\",\"option4\":\"asdasda\"}
单引号替换成双引号
JSON.stringify(params).replace(/‘/g, ‘"‘) 没有亲测
原文地址:https://www.cnblogs.com/wangweizhang/p/12072091.html
时间: 2024-09-28 11:23:25