or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

tessl/npm-ansi-regex

Regular expression for matching ANSI escape codes

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/ansi-regex@6.2.x

To install, run

npx @tessl/cli install tessl/npm-ansi-regex@6.2.0

index.mddocs/

ansi-regex

ansi-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.

Package Information

  • Package Name: ansi-regex
  • Package Type: npm
  • Language: JavaScript (ES modules with TypeScript definitions)
  • Installation: npm install ansi-regex

Core Imports

import ansiRegex from "ansi-regex";

For CommonJS environments:

const ansiRegex = require("ansi-regex").default;
// or
const { default: ansiRegex } = require("ansi-regex");

Basic Usage

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'

Architecture

The ansi-regex library constructs regular expressions that recognize two main types of ANSI escape sequences:

  • OSC (Operating System Command) sequences: Format ESC ] ... ST where ST is a string terminator (BEL, ESC, or 0x9c). Used for advanced terminal features like hyperlinks.
  • CSI (Control Sequence Introducer) sequences: Format 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.

Capabilities

ANSI Regex Generation

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:

  • CSI sequences: Control Sequence Introducer codes for text formatting (colors, styles, cursor control)
  • OSC sequences: Operating System Command codes for advanced terminal features like hyperlinks
  • Standard ECMA-48 codes: Standardized ANSI escape sequences
  • Non-standard codes: Common terminal-specific implementations

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']

Performance and Security Notes

  • The regex is designed to avoid ReDoS (Regular Expression Denial of Service) vulnerabilities
  • For server-side usage with untrusted input, consider implementing timeouts
  • The package maintainers do not consider ReDoS a valid vulnerability for this specific use case
  • Optimized for both performance and comprehensive ANSI code coverage