String
stripANSI

🧼 Removes ANSI escape codes from a string.

Syntax

import { stripANSI } from '@opentf/std';
 
stripANSI(str: string): string

Examples

stripANSI('\u001b[31mHello\u001b[0m')
//=> 'Hello'
 
stripANSI('\x1b[4mUnderline\x1b[0m')
//=> 'Underline'

Implementation Details

This utility uses a comprehensive, ECMA-48 compliant regex designed to strip a wide variety of terminal escape sequences:

  • CSI: Color, style, and cursor control codes.
  • OSC: Operating System Commands, including Hyperlinks and window title updates.
  • 8-bit Codes: Support for legacy 8-bit CSI introducers (\u009B).
  • High Performance: Optimized for speed by correctly handling sequence terminators (BEL, ESC) without being overly greedy.

Try