JavaScript 安全字符串
当我尝试编写这样的 JS 语句时
var a = "\images\avatars\";
,我收到错误 SyntaxError: Unexpected token ILLEGAL
这肯定是因为 \ 符号,但我需要它们。 那么如何才能保证这个字符串的安全呢?
附言。转义,encodeURI 没有帮助
when I try to write such JS statement
var a = "\images\avatars\";
I am getting an error SyntaxError: Unexpected token ILLEGAL
This is definitely because of \ sign, but I need them.
So how can I make this string safe?
PS. escape, encodeURI doesn't help
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
双反斜杠:
如果您将反斜杠包含在字符串文字中,则始终需要双反斜杠。您必须执行类似于在带引号的字符串中嵌入相同类型的引号的操作:
反斜杠用作转义序列。
Double them:
You'll always need to double a backslash if you're including it in a string literal. You'll have to do something similar to embed the same kind of quotes in a quoted string:
The backslash serves as an escape sequence.
逃避它们:
顺便说一句,为什么你需要反斜杠而不是正斜杠?
Escape them:
Why do you need backward slashes rather than forward slashes, by the way?
改为执行此操作。
Do this instead.