Raw text (title or description) that may contain a molarity anywhere within it
The molarity as a normalized string (e.g. "1.5 M", "1-2 M"), or undefined
findMolarity("Potassium Nitrate: EZ-Prep - Makes 150ml of 1.5M Solution") // "1.5 M"
findMolarity("demo kit with 12% hydrogen peroxide, 0.2M potassium iodate") // "0.2 M"
findMolarity("Potassium Iodide Solution, 1M, 500mL") // "1 M"
findMolarity("Sodium chloride, 500 g") // undefined
export const findMolarity = (text: string): string | undefined => {
if (!text || typeof text !== 'string') return undefined;
const match = text.match(MOLARITY_REGEX);
if (!match) return undefined;
const [, low, high, unit] = match;
const value = high ? `${low}-${high}` : low;
return `${value} ${unit}`;
};
Pulls a molar concentration (molarity) out of free-form product copy — the "1.5M", "0.2M", "1M", or "1.5 mol/L" that suppliers bake into titles and descriptions — and returns it as a normalized string suitable for the product's
concentrationfield. The unit must be a capital "M" or literal "mol/L" (seeMOLARITY_REGEX), so a lowercase "m" (milli, e.g. "500ml") is never mistaken for molarity. Returnsundefinedwhen no molarity is present.