简体   繁体   中英

javascript retrieve value from JSON object by matching key using Regex

I have the following javascript object literal (excerpt)

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? ie

Regex = /hello/
foo[Regex]
100

pardon the noob question...

What you have here:

var foo = {"hello[35]":100,"goodbye[45]":42};

is not JSON, 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:

/^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:

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)

Demo: http://jsfiddle.net/asDQm/

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]);
}

http://jsfiddle.net/3qqSr

I am noob here too but I have seen this page previously see it helps you with your question. It basically explains JSon path. Also see this question .

As your JSON is a string, you can use a regexp with this kind of statement:

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

Note that you could use a var instead of "hello" in your regexp.

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
function getVal(s, q){
   var r = s.match(new RegExp(q + "\\[\\d*\\]\":(\\d*)[\\,\\}]"));
   return r?r.pop():false;     
}
getVal(foo, "hello")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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