
您不能只搜索具有部分匹配项的每个字段,所以您必须遍历每个字段,然后检查匹配项的字段名称。尝试这样的事情:
var json = { "content_0_subheading": [ "Title text" ], "content_1_text": [ "Some text" ], "content_2_image": [ [ "http://staging.livelivelyblog.assemblo.com/wp-content/uploads/2013/09/wellbeing-260x130.jpg", 260, 130, true ] ], "content_2_caption": [ "" ]}for (var key in json) { if (json.hasOwnProperty(key)) { if (/content_[0-9]+_image/.test(key)) { console.log('match!', json[key]); // do stuff here! } }}基本上,我们正在做的是:
1)循环遍历json对象的键
for (var key in json)
2)确保json具有该属性,并且我们不访问不需要的键
if (json.hasOwnProperty(key))
3)检查键是否与正则表达式匹配
/content_[0-9]+_image/
图3a)基本上,如果它匹配测试
content_ANY NUMBERS_image,其中
ANY NUMBERS是等于至少一个数位或更多
4)随便使用该数据
console.log(json[key])
希望这可以帮助!