Regular expression for matching ANSI escape codes
npx @tessl/cli install tessl/npm-ansi-regex@6.2.0ansi-regex provides a specialized regular expression for detecting and matching ANSI escape codes in terminal text strings. It generates configurable regex patterns that identify various ANSI escape sequences including CSI sequences for text formatting, colors, and cursor control, as well as OSC sequences for terminal features like hyperlinks.
npm install ansi-regeximport ansiRegex from "ansi-regex";For CommonJS environments:
const ansiRegex = require("ansi-regex").default;
// or
const { default: ansiRegex } = require("ansi-regex");import ansiRegex from "ansi-regex";
// Test for ANSI codes
ansiRegex().test('\u001B[4mcake\u001B[0m');
//=> true
ansiRegex().test('cake');
//=> false
// Extract all ANSI codes
'\u001B[4mcake\u001B[0m'.match(ansiRegex());
//=> ['\u001B[4m', '\u001B[0m']
// Extract only first ANSI code
'\u001B[4mcake\u001B[0m'.match(ansiRegex({onlyFirst: true}));
//=> ['\u001B[4m']
// Remove ANSI codes from text
const cleanText = '\u001B[4mcake\u001B[0m'.replace(ansiRegex(), '');
//=> 'cake'The ansi-regex library constructs regular expressions that recognize two main types of ANSI escape sequences:
ESC ] ... ST where ST is a string terminator (BEL, ESC, or 0x9c). Used for advanced terminal features like hyperlinks.ESC [ ... final-byte or C1 equivalent. Used for text formatting, colors, and cursor control.The regex pattern is optimized to avoid ReDoS vulnerabilities while comprehensively matching both standard ECMA-48 codes and common non-standard terminal implementations.
Creates a regular expression configured to match ANSI escape sequences in text strings.
/**
* Regular expression for matching ANSI escape codes
* @param options - Configuration options for regex behavior
* @returns RegExp configured to match ANSI escape sequences
*/
function ansiRegex(options?: Options): RegExp;
interface Options {
/**
* Match only the first ANSI escape code instead of all occurrences
* @default false
*/
readonly onlyFirst: boolean;
}The function supports matching:
Usage Examples:
import ansiRegex from "ansi-regex";
// Basic matching - finds all ANSI codes
const regex = ansiRegex();
const text = '\u001B[31mRed text\u001B[0m with \u001B[32mgreen\u001B[0m';
const matches = text.match(regex);
// matches: ['\u001B[31m', '\u001B[0m', '\u001B[32m', '\u001B[0m']
// Single match mode
const firstMatch = text.match(ansiRegex({onlyFirst: true}));
// firstMatch: ['\u001B[31m']
// Strip ANSI codes from text
const stripped = text.replace(ansiRegex(), '');
// stripped: "Red text with green"Terminal Hyperlinks:
// OSC 8 hyperlink sequences
const linkText = '\u001B]8;;https://github.com\u0007click\u001B]8;;\u0007';
const linkMatches = linkText.match(ansiRegex());
// linkMatches: ['\u001B]8;;https://github.com\u0007', '\u001B]8;;\u0007']
const linkStripped = linkText.replace(ansiRegex(), '');
// linkStripped: "click"Advanced Color Support:
// RGB color codes with colon separators
const rgbText = '\u001B[38:2:255:0:128mPink text\u001B[0m';
const rgbMatches = rgbText.match(ansiRegex());
// rgbMatches: ['\u001B[38:2:255:0:128m', '\u001B[0m']
// Indexed colors
const indexedText = '\u001B[38:5:196mBright red\u001B[0m';
const indexedMatches = indexedText.match(ansiRegex());
// indexedMatches: ['\u001B[38:5:196m', '\u001B[0m']