Raw text or HTML that may contain a molar mass anywhere within it
The molar mass in g/mol as a positive number, or undefined when none is found
findMolarMass("Molar mass (M) 149,19 g/mol") // 149.19
findMolarMass("MW - 136.169 G/MOL") // 136.169
findMolarMass("M.W. 415.6") // 415.6
findMolarMass("Mp : 288 - 296°C") // undefined
export const findMolarMass = (text: string): number | undefined => {
if (!text || typeof text !== 'string') return undefined;
// Strip markup and decode entities so labels/values/units match on plain text.
const clean = decodeHTMLEntities(text.replace(/<[^>]+>/g, ' '));
const match =
clean.match(MOLAR_MASS_LABELED_UNIT) ??
clean.match(MOLAR_MASS_UNIT_ONLY) ??
clean.match(MOLAR_MASS_LABELED);
if (!match) return undefined;
const value = parseLocalizedNumber(match[1]);
return Number.isFinite(value) && value > 0 ? value : undefined;
};
Finds the first molar mass / molecular weight in a free-form string and returns it as a number. Built to be run over large, messy scraped text the way findFormulaInText is: labels, separators, and unit spellings vary widely across suppliers and locales, so each is matched tolerantly. Matching is tiered by confidence — a labelled value carrying a unit (
Molar mass (M) 149,19 g/mol), then a bare<number> <unit>(58.44 g/mol), then a labelled value with no unit (M.W. 415.6) — and the value is parsed with parseLocalizedNumber so European decimal commas are handled. Returnsundefinedwhen no plausible molar mass is present, so unrelated numbers (melting points, densities, prose) are not mistaken for one.