替换 Javascript 中的引号?
对于我正在制作的网络应用程序,我将收到文本字符串,其中偶尔包含引号。因为我接下来要 document.writing 字符串,所以需要将它们更改为撇号或转义。我该怎么做,因为当我尝试时它似乎不起作用,特别是我认为因为字符串的引号阻止了脚本的其余部分工作。
希望这是有道理的,我对此很陌生,所以这就是为什么它可能没有意义。如果需要的话,我会尽力澄清。谢谢你!
For a web app I'm making, I'm going to be getting text strings coming in, occasionally which contain quotation marks. Because I am then going to be document.writing the string, they need to be changed either into apostrophes or escaped. How would I do that, because when I try it doesn't seem to work, specifically I think because the string's quotation marks stop the rest of the script working.
Hope that makes some sense, I'm quite new to this so that's why it might not make sense. I'll try and clarify if need be. Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对 HTML 进行转义:
对 JS 代码进行转义:
Escaping them for HTML:
Escaping them for JS code:
如果您在服务器上生成 Javascript 字符串,则需要转义引号和某些其他字符。
If you are generating Javascript strings on the server, you will need to escape quotes and certain other characters.
在某些 HTML 中尝试以下代码:
如果只是JS,你可以使用:
这会删除用户提交的值上不需要的引号。
Try the following code in some HTML:
If it's just JS you can use:
This remove de undesired quotes on submited values from user.
您需要像这样转义它们:
因此,如果源字符串具有单引号,请将每个单引号替换为斜杠和单引号。
You need to escape them like so:
So, if the source string has single quotes, replace each single quote with a slash and a single quote.