JavaScript 字符串单引号双引号相互替换
Posted 王维璋
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JavaScript 字符串单引号双引号相互替换相关的知识,希望对你有一定的参考价值。
双引号替换成单引号
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, ‘"‘) 没有亲测
以上是关于JavaScript 字符串单引号双引号相互替换的主要内容,如果未能解决你的问题,请参考以下文章