Toolbox-XToolbox-X

Mask String

Masks a portion of a string for privacy while allowing specified characters at the beginning and/or end to remain visible.

maskString

The maskString function masks a portion of a string for privacy, replacing characters with a mask character while preserving a visible prefix and suffix.

Function Signature

function maskString(input: string, options?: MaskOptions): string

Parameters

  • input (string): The string to mask.
  • options (MaskOptions, optional): Configuration options:
    • start (number): Number of characters to keep visible at the start. Defaults to 1.
    • end (number): Number of characters to keep visible at the end. Defaults to 1.
    • maskCharacter (string): Character used to mask the string. Defaults to '*'.
    • trim (boolean): Whether to trim all whitespace characters before masking. Defaults to false.

Types

MaskOptions

interface MaskOptions {
  start?: number;
  end?: number;
  maskCharacter?: string;
  trim?: boolean;
}

Return Value

  • string: The masked string. If the string is too short to apply the start and end retention options, it is fully masked.

Note

  • The input string is trimmed of all (not just leading and trailing) whitespaces before masking when 'trim' option is true.
  • Ideal for obfuscating sensitive fields like emails, phone numbers, or tokens.

Example Usage

playground.ts

Last updated: Sun, Jun 28, 2026 08:49:40AM (UTC)

On this page