Reputation: 55
I have a strange problem that I am not sure how to solve.
My goal is to be able to split a string that has certain ingredients that are separated by commas into an array so that each element in the array is an ingredient. However, some of the strings that I will come across that list ingredients have a list within the list that looks like this:
WATER, CORN SYRUP AND 2% OR LESS OF EACH OF THE FOLLOWING: CONCENTRATED JUICES (ORANGE, TANGERINE, APPLE, LIME, GRAPEFRUIT), CITRIC ACID, MALIC ACID, ASCORBIC ACID (VITAMIN C), THIAMIN HYDROCHOLORIDE (VITAMIN B1), NATURAL FLAVORS. MODIFIED CORNSTARCH, CANOLA OIL, CELLULOSE GUM, SUCRALOSE, SODIUM HEXAMETAPHOSPHATE, POTASSIUM SORBATE TO PROTECT FLAVOR, YELLOW #5, YELLOW #6 AND DISODIUM EDTA TO PROTECT COLOR.
As you can see, there is a portion of the string that says "2% OR LESS OF EACH OF THE FOLLOWING: CONCENTRATED JUICES (ORANGE, TANGERINE, APPLE, LIME, GRAPEFRUIT)". If the delimiter for the split method is a simple comma, then one of the ingredients will be "2% OR LESS OF EACH OF THE FOLLOWING: CONCENTRATED JUICES (ORANGE" which does not look correct. My goal is to get that entire portion of the string into one element e.g. the element should be "2% OR LESS OF EACH OF THE FOLLOWING: CONCENTRATED JUICES (ORANGE, TANGERINE, APPLE, LIME, GRAPEFRUIT)".
Thank you for taking the time to look at my question!
Upvotes: 1
Views: 49
Reputation: 599
Give this a shot:
\,+(?![^\(]*\))
It seems to work in JavaScript against your example.
Upvotes: 1