如何安全地将 JS 字符串变量包裹在双引号字符中?
显然,当您自己创建实际的字符串文字时,您自己会使用反斜杠转义双引号字符。
var foo = "baz\"bat";
就像您处理少数其他控制字符一样,例如换行符和反斜杠。
var bar = "baz\\bat\nmynew line and a \"quote\" ";
但是,如果您只是将现有变量包装在引号字符中,即将其提供给需要引号输入的其他系统,则会出现一些混乱。
显然,您必须转义字符串中任何潜在的双引号字符。
var doubleQuoteRe = /\"/g;
var quoted = "\"" + unquoted.replace(escaper, '\\\"') + "\"";
但根据一些人的说法,您现在还必须担心变量中转义文字反斜杠字符。换句话说,使用比我的小正则表达式更大的锤子。但是我不明白为什么。
Obviously when you're creating an actual string literal yourself, you backslash escape the double quote characters yourself.
var foo = "baz\"bat";
Just as you would with the handful of other control characters, like linebreaks and backslashes.
var bar = "baz\\bat\nmynew line and a \"quote\" ";
but if you're just wrapping that existing variable in quote character, ie to give it to some other system that requires quoted input, there's some confusion.
Obviously you have to escape any potential double quote characters that are in the string.
var doubleQuoteRe = /\"/g;
var quoted = "\"" + unquoted.replace(escaper, '\\\"') + "\"";
But according to some you also now have to worry about escaping literal backslash characters in the variable. In other words using much bigger hammer than my little regex. However i dont see why.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可能想避免转义已经转义的引号 -
You might want to avoid escaping quotes you already escaped-
答案是,是的,您必须做两件事:
为什么步骤 1 至关重要的最简单解释是考虑 5 个字符的字符串:
在前 3 个字符 (foo) 之后,字符串中有一个文字反斜杠字符,然后是一个文字双引号字符。
(换句话说,作为字符串文字,这看起来像“foo\”“)
如果我只替换引号字符,我最终会得到一个带引号的字符串,其值为
但是这里的两个反斜杠将被解释为 最终会得到不平衡的引号。
因此,当我将此值括在引号中时,如果我先执行步骤 1(将所有反斜杠替换为双反斜杠),
然后执行步骤 2(将引号替换为),则 斜杠引号
现在当我用引号字符包裹我的值时,我终于得到了
正确的结果。
The answer is that yes, you have to do two things:
the simplest explanation for why step 1 is essential, is to consider the 5-character string :
After the first 3 characters (foo), there is a literal backslash character in the string, and then there is a literal double quote character.
(Put another way, as a string literal this would look like "foo\"")
If I were to only replace the quote character, i'd end up with a quoted string whose value was
But the two backslashes here will be interpreted as a single backslash. So when I wrap this value in quotes, I end up with unbalanced quotes.
on the other hand, if I do step 1 first -- replacing all backslashes with double backslashes gives
and then step 2 -- replacing the quote with slash-quote gives
Now when i wrap my value in quote characters i finally get
which is correct.
FF 中有一个非标准的 str.quote()
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/quote
他们建议使用以下polyfill
There is a non standard str.quote() in FF
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/quote
They suggest the following polyfill
您可能想要转义除引号之外的其他字符,例如空白字符(换行符!)和/或非 ASCII 字符。有 Crockford 的
quote()
,可以找到我自己的实现位于 mercurial.intuxication.org。You might want to escape other characters aside from quotes, eg whitespace characters (newlines!) and/or non-ASCII characters. There's Crockford's
quote()
, and my own implementation can be found at mercurial.intuxication.org.