javascript javascript通过使用正则表达式匹配键从JSON对象检索值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8628949/
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
javascript retrieve value from JSON object by matching key using Regex
提问by ejang
I have the following javascript object literal (excerpt)
我有以下 javascript 对象文字(摘录)
var foo = {"hello[35]":100,"goodbye[45]":42};
I have the following query:
我有以下查询:
var query = "hello"
I would like to call foo[query] to obtain the value 100, but there is a [35] for which I don't necessarily know the value of. I know for sure that I will get a unique match. Is there any way to input query is some kind of javascript regular expression? i.e.
我想调用 foo[query] 来获取值 100,但是有一个 [35] 我不一定知道它的值。我确信我会得到一场独一无二的比赛。有什么办法可以输入查询是某种javascript正则表达式吗?IE
Regex = /hello/
foo[Regex]
100
pardon the noob question...
原谅菜鸟的问题...
回答by nnnnnn
What you have here:
你在这里有什么:
var foo = {"hello[35]":100,"goodbye[45]":42};
is notJSON, which is a string representation of an object; what you have is an object literal, which creates an actual JavaScript object. As far as I know the only way to retrieve a value from an object by matching a property name with a regex is to enumerate the property names and test each one. The regex you'll need is something like:
是不JSON,它是一个对象的字符串表示; 您拥有的是一个对象字面量,它创建了一个实际的 JavaScript 对象。据我所知,通过将属性名称与正则表达式匹配来从对象中检索值的唯一方法是枚举属性名称并测试每个属性名称。您需要的正则表达式类似于:
/^hello(\[\d*\])?$/
...which will match against "hello" optionally followed by zero or more digits in square brackets. But you don't want to hard code "hello" given that you also (presumably) need the "goodbye" value, so use a function:
...它将与“hello”匹配,可选地后跟零个或多个方括号中的数字。但是你不想硬编码“hello”,因为你也(大概)需要“goodbye”值,所以使用一个函数:
function getPropertyByRegex(obj,propName) {
var re = new RegExp("^" + propName + "(\[\d*\])?$"),
key;
for (key in obj)
if (re.test(key))
return obj[key];
return null; // put your default "not found" return value here
}
var foo = {"hello[35]":100,"goodbye[45]":42};
alert(getPropertyByRegex(foo, "hello")); // 100
alert(getPropertyByRegex(foo, "goodbye")); // 42
alert(getPropertyByRegex(foo, "whatever")); // null (not found)
回答by Tim Vermaelen
Not sure if you can use regex without any plugins or so ... This might help already ...
不确定你是否可以在没有任何插件的情况下使用正则表达式......这可能已经有所帮助......
var foo = {"hello[35]":100,"goodbye[45]":42};
var query = "hello";
for(var key in foo){
if (key.indexOf(query) > -1)
document.write(foo[key]);
}
回答by Shadow
回答by JMax
As your JSON is a string, you can use a regexp with this kind of statement:
由于您的 JSON 是一个字符串,您可以使用带有这种语句的正则表达式:
var foo = '{"hello[35]":100,"goodbye[45]":42}';
var result = foo.match(/"hello\[\d*\]":\d*/g);
result = result[0].split(":")[1];
alert(result);
See it live on jsfiddle
在jsfiddle 上实时查看
Note that you could use a var instead of "hello" in your regexp.
请注意,您可以在正则表达式中使用 var 代替“hello”。
回答by InuiSadaharu
var foo = {"hello[35]":100,"goodbye[45]":42};
foo = foo.replace(/\[\d+\]/g,'');
var obj = (new Function("return "+foo))();
obj.hello -> 100
obj.goodbye -> 42
var query = 'hello';
obj[query] -> 100
回答by Diode
function getVal(s, q){
var r = s.match(new RegExp(q + "\[\d*\]\":(\d*)[\,\}]"));
return r?r.pop():false;
}
getVal(foo, "hello")