Javascript 比较两个字符串对象的匹配字符串
我只是想知道如何搜索和比较两个字符串对象,并检查其中一个是否包含匹配的字符串 我有一个包含术语和解释的关联数组。 我还将该数组分为两个对象“键”(键显示关联数组的键)和“值”(值显示数组中每个键的值) 我有另一个关联数组,其中包含一个盘及其解释。 我已将这道菜的描述拆分成几个单独的单词,并将它们放入一个对象中 我现在要做的是检查descsplit中的每个单词,搜索术语列表,如果找到,返回对找到的术语的解释。 例如,自由范围包含在盘说明中,检查术语列表中是否有自由范围的匹配项,并返回自由范围的值(说明) 任何帮助都将不胜感激,谢谢Javascript 比较两个字符串对象的匹配字符串,javascript,arrays,regex,object,associative-array,Javascript,Arrays,Regex,Object,Associative Array,我只是想知道如何搜索和比较两个字符串对象,并检查其中一个是否包含匹配的字符串 我有一个包含术语和解释的关联数组。 我还将该数组分为两个对象“键”(键显示关联数组的键)和“值”(值显示数组中每个键的值) 我有另一个关联数组,其中包含一个盘及其解释。 我已将这道菜的描述拆分成几个单独的单词,并将它们放入一个对象中 我现在要做的是检查descsplit中的每个单词,搜索术语列表,如果找到,返回对找到的术语的解释。 例如,自由范围包含在盘说明中,检查术语列表中是否有自由范围的匹配项,并返回自由范围的值(
var TermList= {
'Al dente' : 'Al dente : Pasta cooked until just firm. From the Italian "to the tooth." ',
'Bake' : 'Bake: To cook food in an oven, surrounded with dry heat; called roasting when applied to meat or poultry.',
'Barbecue' : 'Barbecue: To cook foods on a rack or a spit over coals.',
'Baste' : 'Baste: To moisten food for added flavor and to prevent drying out while cooking.',
'Batter' : 'Batter: An uncooked pourable mixture usually made up of flour, a liquid, and other ingredients.',
'Beat' : 'Beat: To stir rapidly to make a mixture smooth, using a whisk, spoon, or mixer.',
'Blanch' : 'Blanch: To cook briefly in boiling water to seal in flavor and color; usually used for vegetables or fruit, to prepare for freezing, and to ease skin removal.',
'Blend' : 'Blend: To thoroughly combine 2 or more ingredients, either by hand with a whisk or spoon, or with a mixer.',
'Boil': 'Boil: To cook in bubbling water that has reached 100 degrees Celcius.',
'Bone' : 'Bone: To remove bones from poultry, meat, or fish.',
'Bouquet garni' : 'Bouquet garni: A tied bundle of herbs, usually parsley, thyme, and bay leaves, that is added to flavor soups, stews, and sauces but removed before serving.',
'Braise' : 'Braise: To cook first by browning, then gently simmering in a small amount of liquid over low heat in a covered pan until tender.',
'Bread': 'Bread: To coat with crumbs or cornmeal before cooking.',
'Free-range': 'Free-range: (Of livestock, especially poultry) kept in natural conditions, with freedom of movement/ (Of eggs) produced by free-range poultry.'
};
var values = []; // Creating an object for the values of the terms in TermList
var keys = []; // Creating an object for the keys of the terms in TermList
//function to assign the keys of terms to object keys.
function showkey() {
for (var key in TermList) {
if (TermList.hasOwnProperty(key)) {
keys.push(key);
}
}
//function that shows the value of each key in TermList.
function showValue(){
for( var value in TermList){
values.push(TermList[value]);
}
showkey();
showValue();
var DishList={
"Chicken and Stuffing Sandwich": "Chicken and Stuffing Sandwich: Succulent Sandwich made from free-range chicken and fresh breadcrumbs mixed with mayonnaise",
"Eggs Benedict": "Poached eggs served with spinach and hollandaise sauce"
};
var descsplit = [];
function SplitDesc() {
for (var value in DishList) {
descsplit.push(DishList[value].split(/[\s.,?!:]+/)); // Splits the values of the key up in Dishlist, and puts them into array.Also makes them avoid punctuations while splitting.
}
}
SplitDesc();
//For every word in descsplit search the TermList and return explanation of term found if found
未完全测试,但这可能有效
var TermList= {
'Al dente' : 'Al dente : Pasta cooked until just firm. From the Italian "to the tooth." ',
'Bake' : 'Bake: To cook food in an oven, surrounded with dry heat; called roasting when applied to meat or poultry.',
'Barbecue' : 'Barbecue: To cook foods on a rack or a spit over coals.',
'Baste' : 'Baste: To moisten food for added flavor and to prevent drying out while cooking.',
'Batter' : 'Batter: An uncooked pourable mixture usually made up of flour, a liquid, and other ingredients.',
'Beat' : 'Beat: To stir rapidly to make a mixture smooth, using a whisk, spoon, or mixer.',
'Blanch' : 'Blanch: To cook briefly in boiling water to seal in flavor and color; usually used for vegetables or fruit, to prepare for freezing, and to ease skin removal.',
'Blend' : 'Blend: To thoroughly combine 2 or more ingredients, either by hand with a whisk or spoon, or with a mixer.',
'Boil': 'Boil: To cook in bubbling water that has reached 100 degrees Celcius.',
'Bone' : 'Bone: To remove bones from poultry, meat, or fish.',
'Bouquet garni' : 'Bouquet garni: A tied bundle of herbs, usually parsley, thyme, and bay leaves, that is added to flavor soups, stews, and sauces but removed before serving.',
'Braise' : 'Braise: To cook first by browning, then gently simmering in a small amount of liquid over low heat in a covered pan until tender.',
'Bread': 'Bread: To coat with crumbs or cornmeal before cooking.',
'Free-range': 'Free-range: (Of livestock, especially poultry) kept in natural conditions, with freedom of movement/ (Of eggs) produced by free-range poultry.'
};
var DishList={
"Chicken and Stuffing Sandwich": "Chicken and Stuffing Sandwich: Succulent Sandwich made from free-range chicken and fresh breadcrumbs mixed with mayonnaise",
"Eggs Benedict": "Poached eggs served with spinach and hollandaise sauce"
};
var keys = Object.keys(TermList);
for(var key in DishList){
var val = DishList[key];
for(var iIndex=0;iIndex<keys.length ;iIndex++){
var term = keys[iIndex];
var regx = new RegExp('\\b'+term+'\\b',"gi");
var found = null;
while((found = regx.exec(val))!=null){
console.log('Found term "'+ term+ '" at index '+found.index);
}
}
}
var-TermList={
“阿尔·登特”:阿尔·登特:意大利面煮到刚硬。从意大利语的“到牙齿”,
“烘焙”:烘焙:在烤箱中用干热烘烤食物;当用于肉类或家禽时称为烘焙,
“烧烤”:烧烤:在架子上或在煤块上烤食物,
“韧皮部”:“韧皮部:湿润食物以增加风味,并防止烹饪时干燥。”,
“面糊”:面糊:通常由面粉、液体和其他成分组成的未煮熟的可浇注混合物,
“搅打”:搅打:用搅拌器、勺子或搅拌器快速搅拌使混合物光滑,
“漂白”:“漂白:在沸水中短时间煮熟,以密封味道和颜色;通常用于蔬菜或水果,以备冷冻,并便于去皮。”,
“混合”:混合:用手用搅拌器或勺子将2种或2种以上的成分彻底混合,
“煮沸”:煮沸:在达到摄氏100度的沸水中煮,
“骨头”:骨头:从家禽、肉或鱼身上取下骨头,
“花束加尼”:花束加尼:一束绑在一起的草药,通常是欧芹、百里香和月桂叶,添加到调味汤、炖菜和酱汁中,但在食用前去掉,
“炖”:炖:先煮成褐色,然后在有盖的平底锅中用少量液体小火炖至变软,
“面包”:面包:在烹饪前涂上面包屑或玉米粉,
“自由放养”:“自由放养:(指家畜,尤其是家禽)在自然条件下饲养,自由放养家禽生产的蛋可以自由移动。”
};
var目录={
“鸡肉和填料三明治”:“鸡肉和填料三明治:由自由放养的鸡肉和新鲜面包屑混合蛋黄酱制成的多汁三明治”,
“本笃会鸡蛋”:“水煮鸡蛋配菠菜和荷兰酱”
};
var keys=Object.keys(术语列表);
for(列表中的var键){
var val=DishList[键];
对于(var iIndex=0;iIndexSomething,如术语列表中的“自由范围”){…
大写和标点符号很重要:“自由范围”!=“自由范围”!=“自由范围”
。