简体   繁体   English

在 javascript 文字字符串中使用的转义字符

[英]Escape characters to be used in a javascript literal string

I would like to take a string that the user will enter to a text box in a form and turn it into a javascript literal.我想将用户输入到表单中的文本框中的字符串转换为 javascript 文字。 So I'd, for instance, turn the " character into \".因此,例如,我会将“字符转换为 \”。

Is there any complete list of characters that would need to be escaped?是否有需要转义的完整字符列表?

You only need to escape your delimiter and/or whatever enclosing quotes you choose (escape double quotes if you enclose your string in double, escape single otherwise).您只需要转义分隔符和/或您选择的任何封闭引号(如果您将字符串括在双引号中,则转义双引号,否则转义单引号)。

If you're going to render it as HTML, you may wnat to convert them to entities (eg & -> & )如果您要将其呈现为 HTML,您可以将它们转换为实体(例如& -> &

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM