DEV Community

Shahryar Ahmad
Shahryar Ahmad

Posted on Fully Autonomous

Parsing cosmetic ingredient labels in JavaScript (and an open comedogenic dataset)

A cosmetic ingredient list looks like CSV until you try to split it. Here is a real-looking one:

Ingredients: Aqua (Water), Cocos Nucifera (Coconut) Oil, Isopropyl Myristate 5%,
Cetearyl Alcohol, Glycerin, D&C Red No. 30 Aluminum Lake, Parfum (Fragrance).
Enter fullscreen mode Exit fullscreen mode

label.split(",") almost works, but labels put commas inside brackets (Tocopherol (Vitamin E, Antioxidant)), use semicolons or bullets, add percentages and "may contain" sections, and write the same colour three ways. This is the parser behind a free pore-clogging ingredient checker, cut down to the parts that matter.

1. Split on separators, but only outside brackets

function split(text) {
  let t = text.replace(/\r/g, "\n");
  t = t.replace(/\b(?:active|inactive|other)\s+ingredients?\s*:/gi, ",")
       .replace(/\bingredients?\s*:/gi, ",");
  t = t.replace(/may contain\s*:?/gi, ",").replace(/\+\s*\/\s*-/g, ",").replace(/[\[\]{}]/g, ",");
  t = t.replace(/\d+(?:[.,]\d+)?\s*%/g, " "); // drop "5%"

  const parts = [];
  let cur = "", depth = 0;
  for (const ch of t) {
    if (ch === "(") depth++;
    if (ch === ")" && depth > 0) depth--;
    if (depth === 0 && /[,;\n•|·]/.test(ch)) { parts.push(cur); cur = ""; }
    else cur += ch;
  }
  parts.push(cur);
  return parts.map((p) => p.replace(/\s+/g, " ").trim()).filter((p) => p.length > 1);
}
Enter fullscreen mode Exit fullscreen mode

The depth counter is the whole trick: a comma inside ( ) belongs to the ingredient. +/- (the EU "may contain" marker) and square brackets become separators, and headings such as "Inactive ingredients:" are swallowed.

2. Normalise names so the same thing matches once

function norm(s) {
  s = s.toLowerCase().replace(/[‐‑–]/g, "-");
  s = s.replace(/\([^)]*\)/g, " ");                       // drop "(Coconut)"
  s = s.replace(/\bd\s*&\s*c\b/g, "").replace(/\bfd\s*&\s*c\b/g, "");
  s = s.replace(/\bred\s*(?:no\.?|#)?\s*(\d+)(?:\s+(?:aluminum|aluminium|calcium|barium)?\s*lake)?/g, "red $1");
  s = s.replace(/[*†‡.]/g, " ").replace(/-/g, " ");
  return s.replace(/\s+/g, " ").trim();
}

norm("D&C Red No. 30 Aluminum Lake"); // "red 30"
norm("Cocos Nucifera (Coconut) Oil");  // "cocos nucifera oil"
Enter fullscreen mode Exit fullscreen mode

Colours are the worst offenders: D&C Red No. 30, Red 30 and Red 30 Aluminum Lake are the same pigment on different labels, so the lake salt and the "D&C" prefix are stripped before lookup.

3. Try the name, then what is in the brackets

function lookup(raw, index) {
  const tries = [norm(raw)];
  for (const m of raw.matchAll(/\(([^)]*)\)/g)) tries.push(norm(m[1]));
  tries.push(norm(raw.replace(/[()]/g, " ")));
  for (const k of tries) if (k && Object.hasOwn(index, k)) return index[k];
  return -1;
}
Enter fullscreen mode Exit fullscreen mode

Parfum (Fragrance) might be listed under either name, so the bracket contents get their own try.

Result

Run the label above through it against Fulton's 1989 comedogenicity table:

Label entry Matched Comedogenic (0 to 5) Irritancy
Cocos Nucifera (Coconut) Oil Coconut butter 4 0
Isopropyl Myristate Isopropyl myristate 5 3
Cetearyl Alcohol Cetearyl alcohol 2 1
Glycerin Glycerin 0 0
D&C Red No. 30 Aluminum Lake D&C red #30 3 0

Water and fragrance are not in the table, which is the honest answer rather than a zero.

The data

I published the 219-row table as CSV and JSON: comedogenic-ingredients on GitHub, with a filterable page at shehrii.github.io/comedogenic-ingredients. It is transcribed from Fulton JE Jr., Comedogenicity and irritancy of commonly used ingredients in skin care products, J. Soc. Cosmet. Chem. 40 (1989).

One caveat if you build on it: the grades come from rabbit ears, which react more than human skin, and Fulton found results changed with the supplier and the concentration. Treat a high grade as "look closer", not as a verdict on a product.

Top comments (0)