2017-06-29 43 views
1

我今天下午在做的練習上有些迷茫。 我有一些信息到一個JSON數組,像這樣:如何使用正則表達式搜索JSON

var json = [ 
 
    { 
 
    "recipe": { 
 
     "URL": "www.google.com", 
 
     "Title": "theTitle", 
 
     "Time": "theTime", 
 
     "Ingredients": "... tomato potato orange ...", 
 
     "Image": "theImage" 
 
    } 
 
    }, 
 
    { 
 
    "recipe": { 
 
     "URL": "www.google.com", 
 
     "Title": "theTitle", 
 
     "Time": "theTime", 
 
     "Ingredients": "... tomato orange potato and fish...", 
 
     "Image": "theImage" 
 
    } 
 
    }, 
 
    { 
 
    "recipe": { 
 
     "URL": "www.google.com", 
 
     "Title": "theTitle", 
 
     "Time": "theTime", 
 
     "Ingredients": "... nothing ...", 
 
     "Image": "theImage" 
 
    } 
 
    }, 
 
    { 
 
    "recipe": { 
 
     "URL": "www.google.com", 
 
     "Title": "theTitle", 
 
     "Time": "theTime", 
 
     "Ingredients": "... nothing ...", 
 
     "Image": "theImage" 
 
    } 
 
    } 
 
]

而我試圖讓在JSON的位置,如果它與我的變量之一匹配。這裏是我想要做的一個例子:

var matches = ["tomato","potato"] 
 

 
for (var i = 0; i < json.length;i++){ 
 
if (json[i].recipe.Ingredients == matches) { 
 
alert("I got something :" + json[i]) 
 
} 
 
else { 
 
nothing} 
 
}

所以,我試圖把它與一個正則表達式,但沒有奏效。 任何想法,我該怎麼做呢? 對不起,如果它可能看起來很愚蠢,我還是新來的編碼:D!

+0

您可以使用[String.prototype.indexOf()]查看另一個子字符串是否存在(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf ) –

+0

你有解決這個任務與'RegExp'?我認爲這不合適。 – Hitmands

回答

2

的溶液可以是:

new RegExp(matches.join('|')).test(json[i].recipe.Ingredients) 

其中:

  1. matches.join( '|') - > 「番茄|馬鈴薯」

  2. 新正則表達式(比賽.join('|')) - >/tomato | potato /(正則表達式)

  3. json [i] .recipe.Ingredients - >是內容成分

  4. test該方法執行搜索正則表達式和指定字符串之間的匹配。返回true或false。

var json = [ 
 
    { 
 
     "recipe": { 
 
      "URL": "www.google.com", 
 
      "Title": "theTitle", 
 
      "Time": "theTime", 
 
      "Ingredients": "... tomato potato orange ...", 
 
      "Image": "theImage" 
 
     } 
 
    }, 
 
    { 
 
     "recipe": { 
 
      "URL": "www.google.com", 
 
      "Title": "theTitle", 
 
      "Time": "theTime", 
 
      "Ingredients": "... tomato orange potato and fish...", 
 
      "Image": "theImage" 
 
     } 
 
    }, 
 
    { 
 
     "recipe": { 
 
      "URL": "www.google.com", 
 
      "Title": "theTitle", 
 
      "Time": "theTime", 
 
      "Ingredients": "... nothing ...", 
 
      "Image": "theImage" 
 
     } 
 
    }, 
 
    { 
 
     "recipe": { 
 
      "URL": "www.google.com", 
 
      "Title": "theTitle", 
 
      "Time": "theTime", 
 
      "Ingredients": "... nothing ...", 
 
      "Image": "theImage" 
 
     } 
 
    } 
 
]; 
 
var matches = ["tomato", "potato"] 
 

 
for (var i = 0; i < json.length; i++) { 
 
    if (new RegExp(matches.join('|')).test(json[i].recipe.Ingredients)) { 
 
     console.log("I got something :" + JSON.stringify(json[i])) 
 
    } 
 
    else { 
 
     console.log('nothing'); 
 
    } 
 
}

0

這裏是無需使用Regex我的解決方案。

var json = [ 
 
    { 
 
    "recipe": { 
 
     "URL": "www.google.com", 
 
     "Title": "theTitle", 
 
     "Time": "theTime", 
 
     "Ingredients": "... tomato potato orange ...", 
 
     "Image": "theImage" 
 
    } 
 
    }, 
 
    { 
 
    "recipe": { 
 
     "URL": "www.google.com", 
 
     "Title": "theTitle", 
 
     "Time": "theTime", 
 
     "Ingredients": "... tomato orange potato and fish...", 
 
     "Image": "theImage" 
 
    } 
 
    }, 
 
    { 
 
    "recipe": { 
 
     "URL": "www.google.com", 
 
     "Title": "theTitle", 
 
     "Time": "theTime", 
 
     "Ingredients": "... nothing ...", 
 
     "Image": "theImage" 
 
    } 
 
    }, 
 
    { 
 
    "recipe": { 
 
     "URL": "www.google.com", 
 
     "Title": "theTitle", 
 
     "Time": "theTime", 
 
     "Ingredients": "... nothing ...", 
 
     "Image": "theImage" 
 
    } 
 
    } 
 
] 
 

 

 
var matches = ["tomato","potato"] 
 

 
for (var i = 0; i < json.length;i++){ 
 
    matches.forEach(function(word){ 
 
    if(json[i].recipe.Ingredients.indexOf(word) > -1){ 
 
    console.log(json[i]); 
 
    } else { 
 
    console.log("doesn't exist"); 
 
    } 
 
    }); 
 
}

0

多虧了你們兩個! 最後一個問題,我的JSON將獲得大約60K項目。你認爲我可以繼續使用這些搜索方法,還是我絕對需要構建API?