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

    Function subscriptToAscii

    • Converts subscript Unicode digits back to regular ASCII numbers — the inverse of subscript. Turns a display-formatted formula like "Na₆O₁₈P₆" back into "Na6O18P6" before a lookup that only understands plain digits (e.g. a PubChem query, or formula detection). Non-subscript characters pass through unchanged.

      Parameters

      • str: string

        The string that may contain subscript digits

      Returns string

      The string with subscript digits converted to ASCII digits

      subscriptToAscii("Na₆O₁₈P₆") // "Na6O18P6"
      subscriptToAscii("H₂O") // "H2O"
      export const subscriptToAscii = (str: string) => {
      // Subscript digits are contiguous from U+2080 (₀), so the digit is codePoint − 0x2080.
      return str.replace(/[₀-₉]/g, (c) => String(c.charCodeAt(0) - 0x2080));
      };