---
title: API Reference
product: version-5-0-0-regex
doc_type: api_reference
version: v3.0.1
source: git2docs (code-derived, validation-filtered)
canonical: https://git2docs.com/source2books/docs/version-5-0-0-regex/ansi-regular-expression/api-reference
---

# API Reference

_Complete API documentation for all public classes, functions, and types_

## Description

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.

## Parameters

| Parameter | Type | Required | Description |
|-----------|------|----------|-------------|
| *(none)* | — | — | `ansiRegex()` accepts no parameters. Call it directly to receive the compiled regex. |

## Returns

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 `RegExp` instance is returned each time you call `ansiRegex()`. Reuse the returned instance within a given operation, but call `ansiRegex()` again if you need a fresh regex with its `lastIndex` reset to `0`.

## Errors

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

## Examples

**Check whether a string contains any ANSI escape codes**

```js
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**

```js
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**

```js
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()`**

```js
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
```

## Notes

- **New instance per call:** Every invocation of `ansiRegex()` constructs and returns a brand-new `RegExp` object. If you plan to use the regex multiple times in the same logical operation, store the result in a variable rather than calling `ansiRegex()` repeatedly.
- **Global flag and `lastIndex`:** Because the returned regex uses the `g` flag, its `lastIndex` property advances with each `exec()` call on the same instance. If you reuse the same instance across unrelated strings without resetting `lastIndex` to `0`, you may get unexpected results. Calling `ansiRegex()` 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 with `require('ansi-regex')`. If you are working in an ESM context, use a dynamic `import()` or a bundler that handles CommonJS interop.
