[英]Escape string in javascript
I need to escape this string somehow (with newlines): 我需要以某种方式(使用换行符)转义该字符串:
:-\\
:-\\
"123"“ 123”
:-/:-/
And get this after escaping: 并在转义后得到这个:
:-\\\\n\\"123\\"\\n:-/
:-\\\\ n \\“ 123 \\” \\ n:-/
I tried this, but it's not what I needed. 我试过了,但这不是我所需要的。
:-\\\n/"123/"\n:-/ .replace(/"/g,'/\"')
:-\\\n/"123/"\n:-/ .replace(/"/g,'\/"')
:-\\\n\\"123\\"\n:-/ .replace(/"/g, '\\"') ???
Don't mess with replaces, use JSON.stringify
: 不要
JSON.stringify
替换,使用JSON.stringify
:
text = document.querySelector('pre').textContent; text = JSON.stringify(text).slice(1, -1) alert(text)
<pre> :-\\ "123" :-/ </pre>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.