Function numericLiteral

  • Match the literal representations of numbers in JavaScript code.

    You can use Number(token.content.replaceAll(numericSeparator, '')) to get the numeric value. The default numeric separator is _, you can customize it by setting options.numericSeparator to a string.

    If you want to disable the numeric separator, set options.numericSeparator to false.

    If options.acceptInvalid is true (by default), common invalid numeric literals will also be matched and marked with error.

    E.g.

    • Valid numeric literals
      • 42
      • 3.1415
      • 1.5e10
      • 0.123e-4
      • 0x2a
      • 0xFF
      • 0o755
      • 1_000_000
      • 1_000_000.000_001
      • 1e6_000
    • Invalid numeric literals
      • 0o[0-7]*[^0-7]+: Octal literals that include non-octal characters.
      • 0x[\da-f]*[^\da-f]+: Hexadecimal literals that include non-hexadecimal characters.
      • (?:\d+\.){2,}: Numeric literals that include more than one decimal point.
      • \d+\.\.\d+: Numeric literals that include more than one decimal point without any other characters in between.
      • \d+e[+-]?\d+e[+-]?\d+: Numeric literals that include more than one exponent (e or E).
      • \d+e: Numeric literals that end with an exponent but without any digits after the exponent symbol.

    Parameters

    • Optional options: {
          acceptInvalid?: boolean;
          boundary?: boolean;
          invalidError?: any;
          numericSeparator?: string | false;
      }
      • Optional acceptInvalid?: boolean

        If true (by default), common invalid numeric literals will also be accepted and marked with options.invalidError.

      • Optional boundary?: boolean

        If true (by default), the numeric literal must have a boundary at the end (non inclusive).

      • Optional invalidError?: any

        Default: "invalid numeric literal"

      • Optional numericSeparator?: string | false

    Returns Action

Generated using TypeDoc