I have this list of ingredients I am trying to make a regex to look for 1 cup
, or 1 tsp
or 1 tablespoon and so on.....
I have made this regex but It doesn't work as well. I am trying separate ingredients from the measurements.
So with this string 1 Chopped Tomato
it should take out the 1
as amount and output this:
const output = [
{
val: "Chopped Tomato",
amount: "1",
},
And with this string below it should be able to take out ½ tsp
from ½ tsp fine salt
and output this:
const output = [
{
val: "fine sea salt",
amount: "½ tsp",
},
These are the values I am using for the measurements:
const measures = [
"tbsp","tablespoon","tsp","teaspoon","oz","ounce","fl. oz","fluid ounce","cup","qt",
"quart","pt","pint","gal","gallon","mL","ml","milliliter","g","grams","kg","kilogram","l","liter",
];
This is the input and regex I built
const Ingris = [
"1 teaspoon heavy cream",
"1 Chopped Tomato",
"1/2 Cup yogurt",
"1 packet pasta ",
"2 ounces paprika",
]
const FilterFunction = (term) => {
let data = []
if (term) {
const newData = Ingris.filter(({
ingridients
}) => {
if (RegExp(term, "gim").exec(ingridients))
return ingridients.filter(({
val
}) =>
RegExp(term, "gim").exec(val)
).length;
})
data.push(newData)
} else {
data = []
}
};
console.log(FilterFunction("cup"))
Desired Output:
const output = [
{
val: "Tomato",
amount: "1 Chopped ",
},
{
val: "yogurt",
amount: "1/2 Cup",
},
{
val: "1",
amount: "packet pasta ",
},
{
val: "fine sea salt",
amount: "½ tsp",
},
{
val: "heavy cream",
amount: "1/2 teaspoon",
},
{
val: "paprika",
amount: "2 ounces",
},
];