API Reference
Complete API documentation for all public classes, functions, and types
The ansiRegex() function generates a regular expression that matches ANSI escape codes in strings. Use it when you need to detect, strip, or otherwise process ANSI escape sequences — such as color codes or cursor movement commands — from terminal output or log data.
| Parameter | Type | Required | Description |
|---|---|---|---|
| (none) | — | — | ansiRegex() accepts no parameters. Call it directly to receive the compiled regex. |
Returns a RegExp object compiled with the global flag (g). The pattern matches all ANSI escape sequences in a target string, including CSI sequences (e.g., color and formatting codes beginning with ESC [ or \u009B) and OSC sequences terminated by BEL (\u0007). Because the regex is created with the g flag, you can use it with methods like String.prototype.match(), String.prototype.replace(), or RegExp.prototype.exec() in a loop to find all occurrences in a given string.
Important: A new
RegExpinstance is returned each time you callansiRegex(). Reuse the returned instance within a given operation, but callansiRegex()again if you need a fresh regex with itslastIndexreset to0.
Under normal usage ansiRegex() does not throw. No arguments are accepted, so there is no risk of type-related errors from unexpected input.
| Error | When it occurs |
|---|---|
| (none documented) | The function has no known error conditions. |
Check whether a string contains any ANSI escape codes
const ansiRegex = require('ansi-regex');
const str = '\u001B[4mHello, World!\u001B[0m';
console.log(ansiRegex().test(str));
// => true
Extract all ANSI escape sequences from a string
const ansiRegex = require('ansi-regex');
const str = '\u001B[31mError:\u001B[0m Something went wrong';
const matches = str.match(ansiRegex());
console.log(matches);
// => [ '\u001B[31m', '\u001B[0m' ]
Strip ANSI escape codes from a string
const ansiRegex = require('ansi-regex');
const raw = '\u001B[32mSuccess\u001B[0m: Operation complete';
const clean = raw.replace(ansiRegex(), '');
console.log(clean);
// => 'Success: Operation complete'
Iterate over all matches using exec()
const ansiRegex = require('ansi-regex');
const str = '\u001B[1mBold\u001B[0m and \u001B[4mUnderline\u001B[0m';
const regex = ansiRegex();
let match;
while ((match = regex.exec(str)) !== null) {
console.log(`Found: ${JSON.stringify(match[0])} at index ${match.index}`);
}
// => Found: "\u001B[1m" at index 0
// => Found: "\u001B[0m" at index 8
// => Found: "\u001B[4m" at index 14
// => Found: "\u001B[0m" at index 26
- New instance per call: Every invocation of
ansiRegex()constructs and returns a brand-newRegExpobject. If you plan to use the regex multiple times in the same logical operation, store the result in a variable rather than callingansiRegex()repeatedly. - Global flag and
lastIndex: Because the returned regex uses thegflag, itslastIndexproperty advances with eachexec()call on the same instance. If you reuse the same instance across unrelated strings without resettinglastIndexto0, you may get unexpected results. CallingansiRegex()again is the safest way to obtain a clean instance. - Covered sequence types: The regex is designed to handle the two most common families of ANSI escape sequences: C1 control sequences (CSI, started by
ESC [or the 8-bit equivalent\u009B) and OSC sequences terminated by the BEL character (\u0007). Less common or proprietary terminal extensions may not be matched. - CommonJS module: The source uses
module.exports, so you import the function withrequire('ansi-regex'). If you are working in an ESM context, use a dynamicimport()or a bundler that handles CommonJS interop.