<input id="testInput" type="hidden"/>
const a = `L'hotel+-"+-\<>ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿΑαΒβΓγΔδΕεΖζΗηΘθΙιΚκΛλΜμΝνΞξΟοΠπΡρΣσΤτΥυΦφΧχΨψΩω`
console.log('before', a)
console.log('after', escapeStr(a))
const jsonValue = { name: escapeStr(a) }
const jsonStringify = JSON.stringify(jsonValue)
console.log('JSON before', jsonValue)
console.log('JSON stringify', jsonStringify)
console.log('JSON parse', JSON.parse(jsonStringify))
console.log(`result:${JSON.parse(jsonStringify)['name']}`)
document.querySelector('#testInput').value = JSON.parse(jsonStringify)['name']
function escapeStr(str) {
const convertMap = {
'"': "\"",
"'": "\'"
}
for (let [key, value] of Object.entries(convertMap)) {
str = str.replace(new RegExp(key, "g"), value)
}
return str
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.