ChemPal Documentation - v1.6.0
    Preparing search index...

    Function extractSmiles

    • Extracts every SMILES structure embedded in a block of free text, in order of appearance. Each candidate must contain a "strong" structural character (a bond, branch, bracket, or digit) so ordinary words and chemical names are not misread as structures. Bracketed single atoms such as [Na+] are returned, but bare single-character atoms are not (a stray P is not phosphorus). Because SMILES branch parentheses are indistinguishable from prose parentheses, a structure wrapped directly in text parentheses may include a trailing ).

      Parameters

      • text: string

        Arbitrary text that may contain one or more SMILES structures

      Returns string[]

      The extracted SMILES strings, in order of appearance (empty if none found)

      extractSmiles("The product CC(=O)O forms first.") // ["CC(=O)O"]
      extractSmiles("Compare CC(=O)O and CN1C=NC2=C1C(=O)N(C)C(=O)N2C here.")
      // ["CC(=O)O", "CN1C=NC2=C1C(=O)N(C)C(=O)N2C"]
      extractSmiles("no structures in this sentence") // []
      export function extractSmiles(text: string): string[] {
      const found: string[] = [];
      for (const match of text.matchAll(SMILES_EXTRACT)) {
      const value = match.groups?.SMILES;
      if (value) found.push(value);
      }
      return found;
      }