Skip to content
VaelJS Blog

vael:fmt/strip-ansi-code

Strip ANSI escape sequences from strings.

Available in both vjs (vaeljs) and vjs-lite (vaeljs-lite) runtimes. Removes CSI, OSC, and SGR escape sequences while preserving visible text and whitespace.

import ... from "vael:fmt/strip-ansi-code"
Since v0.0.0-alpha.2 vjs (V8) vjs-lite (QuickJS)
Cargo feature: vael-fmt (enabled by default in vaeljs-core and vaeljs-lite-core)
import { stripAnsiCode } from "vael:fmt/strip-ansi-code";
const colored = "\x1b[31mError\x1b[0m: Something failed";
console.log(stripAnsiCode(colored)); // "Error: Something failed"
functionSince v0.0.0-alpha.2vjs & vjs-lite
export function stripAnsiCode(text: string): string;

Remove all ANSI escape sequences from text.

Parameters
NameTypeDescription
textstringInput string potentially containing ANSI escape sequences.
Returns
string — Plain string with ANSI escape sequences removed.
Exceptions
  • TypeError — when text is not a string.
import { stripAnsiCode } from "vael:fmt/strip-ansi-code";
console.log(stripAnsiCode("\x1b[1m\x1b[32mOK\x1b[0m")); // "OK"