Oracle, mysql and sqlite all have quote function. var thing = quote("so \"weird\" and thing's stuff") may work.
It seems code is transformed one way. It's very difficult to reverse if only single and double quotes are escaped by backslash because we wouldn't know which one to remove.
A better way that can perform bidirectional transformation of plain as well as binary in safe way is base64 encoding.
echo \'\"\' \"\'\" \\
'"' "'" \
echo \'\"\' \"\'\" \\ | base64
JyInICInIiBcCg==
echo JyInICInIiBcCg== | base64 -d
'"' "'" \
can be compressed to save space.
wc -c /usr/bin/bashbug
6957
gzip < /usr/bin/bashbug | base64 |wc -c
4094