Strip ANSI escape codes from a string
npx @tessl/cli install tessl/npm-strip-ansi@7.1.0Strip ANSI provides a lightweight utility for removing ANSI escape codes from strings. It offers a simple, single-function API that accepts any string and returns a clean version with all ANSI formatting codes (colors, styles, cursor movements, etc.) stripped out, making it essential for processing terminal output and colored text in Node.js applications.
npm install strip-ansiimport stripAnsi from 'strip-ansi';import stripAnsi from 'strip-ansi';
// Remove ANSI codes from colored text
stripAnsi('\u001B[4mUnicorn\u001B[0m');
//=> 'Unicorn'
// Remove ANSI codes from terminal links
stripAnsi('\u001B]8;;https://github.com\u0007Click\u001B]8;;\u0007');
//=> 'Click'
// Process complex terminal output
const terminalOutput = '\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m';
const cleanText = stripAnsi(terminalOutput);
//=> 'foofoo'Removes all ANSI escape codes from a string while preserving the original text content.
/**
* Strip ANSI escape codes from a string
* @param string - The input string containing ANSI escape codes to be stripped
* @returns The input string with all ANSI escape codes removed
* @throws TypeError when input is not a string type
*/
export default function stripAnsi(string: string): string;Parameters:
string (string, required): The input string containing ANSI escape codes to be strippedReturns:
string: The input string with all ANSI escape codes removedError Handling:
Expected a \string`, got `${typeof input}``Supported ANSI Codes:
\u001B]8;;\u0007)Usage Examples:
import stripAnsi from 'strip-ansi';
// Basic color removal
stripAnsi('\u001B[31mRed text\u001B[0m');
//=> 'Red text'
// Complex styling removal
stripAnsi('\u001B[0;33;49;3;9;4mbar\u001B[0m');
//=> 'bar'
// Terminal link removal
stripAnsi('\u001B]8;;https://example.com\u0007Link text\u001B]8;;\u0007');
//=> 'Link text'
// Input validation
try {
stripAnsi(123);
} catch (error) {
console.log(error.message);
//=> 'Expected a `string`, got `number`'
}
// Common use cases
const logOutput = '\u001B[32m[INFO]\u001B[0m \u001B[1mOperation completed\u001B[0m';
const cleanLog = stripAnsi(logOutput);
//=> '[INFO] Operation completed'
// Processing command line output
const cliOutput = '\u001B[00;38;5;244m\u001B[m\u001B[00;38;5;33mfilename.js\u001B[0m';
const filename = stripAnsi(cliOutput);
//=> 'filename.js'Implementation Details:
ansi-regex library for comprehensive ANSI code pattern matching