javascript 删除javascript中的双引号?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16858155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:15:31  来源:igfitidea点击:

Remove double quotes in javascript?

javascriptarrays

提问by Mohd Anas

How to remove double quotes from array in JavaScript?

如何从 JavaScript 中的数组中删除双引号?

Suppose this is an array

假设这是一个数组

enc= ["WPA2", "WPA2", "WPA2", "WPA2", "WPA1", "WEP", "WPA2", "WPA2", "WPA1", "WEP", "WEP"]

Thanks

谢谢

Any help would be appreciated.

任何帮助,将不胜感激。

回答by Quentin

There are no double quotes in that array. The quotes just delimit the string literals, when they are parsed into strings they don't have quotes in them.

该数组中没有双引号。引号只是分隔字符串文字,当它们被解析为字符串时,它们中没有引号。



If you wanted to remove all the quotes from a string which actually had some in it:

如果您想从字符串中删除所有引号,其中实际上有一些引号:

str = str.replace(/"/g, "");  // RegEx to match `"` characters, with `g` for globally (instead of once)

You could do that in a loop over an array:

你可以在一个数组的循环中做到这一点:

for (var i = 0; i < enc.length; i++) {
    enc[i] = enc[i].replace(/"/g, "");
}


If you wanted to change the source code so that it looked like:

如果您想更改源代码,使其看起来像:

enc= [WPA2, WPA2, WPA2, WPA2, WPA1, WEP, WPA2, WPA2, WPA1, WEP, WEP]

… (and populated the array with some predefined variables) then you would be too late. The source code would have already been parsed by the JavaScript engine.

...(并用一些预定义的变量填充数组)那么你就太晚了。JavaScript 引擎已经解析了源代码。

To get at variables when you have their names in strings, you would have to enter the murkey world of variable variablesand are better off refactoring to use object properties (or going direct to an array).

要在字符串中包含变量名称时获取变量,您必须进入变量变量的模糊世界,并且最好重构以使用对象属性(或直接访问数组)。

回答by Thomas Junk

This would be an error. Quotes are needed to mark the content beeing a string, not a variable.

这将是一个错误。需要引号将内容标记为字符串,而不是变量。