ansi-regex

中文文档
6.5.0发布于 2026/10/29MITTypeScriptES Module零依赖近 30 天下载 20亿

Regular expression for matching ANSI escape codes

原始使用文档
文档目录8 节
  1. Install
  2. Usage
  3. API
  4. ansiRegex(options?)
  5. Important
  6. FAQ
  7. Why do you test for codes not in the ECMA 48 standard?
  8. Maintainers

ansi-regex

Regular expression for matching ANSI escape codes

Install

npm install ansi-regex

Usage

import ansiRegex from 'ansi-regex';

ansiRegex().test('\u001B[4mcake\u001B[0m');
//=> true

ansiRegex().test('cake');
//=> false

'\u001B[4mcake\u001B[0m'.match(ansiRegex());
//=> ['\u001B[4m', '\u001B[0m']

'\u001B[4mcake\u001B[0m'.match(ansiRegex({onlyFirst: true}));
//=> ['\u001B[4m']

'\u001B]8;;https://github.com\u0007click\u001B]8;;\u0007'.match(ansiRegex());
//=> ['\u001B]8;;https://github.com\u0007', '\u001B]8;;\u0007']

API

ansiRegex(options?)

Returns a regex for matching ANSI escape codes.

options

Type: object

onlyFirst

Type: boolean
Default: false (Matches any ANSI escape codes in a string)

Match only the first ANSI escape.

Important

If you run the regex against untrusted user input in a server context, you should give it a timeout.

I do not consider ReDoS a valid vulnerability for this package.

FAQ

Why do you test for codes not in the ECMA 48 standard?

Some of the codes we run as a test are codes that we acquired finding various lists of non-standard or manufacturer specific codes. We test for both standard and non-standard codes, as most of them follow the same or similar format and can be safely matched in strings without the risk of removing actual string content. There are a few non-standard control codes that do not follow the traditional format (i.e. they end in numbers) thus forcing us to exclude them from the test because we cannot reliably match them.

On the historical side, those ECMA standards were established in the early 90's whereas the VT100, for example, was designed in the mid/late 70's. At that point in time, control codes were still pretty ungoverned and engineers used them for a multitude of things, namely to activate hardware ports that may have been proprietary. Somewhere else you see a similar 'anarchy' of codes is in the x86 architecture for processors; there are a ton of "interrupts" that can mean different things on certain brands of processors, most of which have been phased out.

Maintainers

版本与发布记录

ansi-regex 共发布 24 个版本,首次发布于 2014/06/04;过去 12 个月发布了 3 个版本。

版本发布日期距今
6.5.0latest2026/10/296 天前
6.4.02026/09/271 个月前
6.3.02026/08/122 个月前
6.2.22025/09/081.2 年前
6.2.12025/09/081.2 年前
6.2.02025/08/181.2 年前

关于 ansi-regex 的常见问题

ansi-regex 是什么?

Regular expression for matching ANSI escape codes

如何安装 ansi-regex?

在项目目录运行 npm install ansi-regex,或使用 pnpm add / yarn add / bun add。

ansi-regex 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

ansi-regex 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

ansi-regex 需要什么版本的 Node.js?

package.json 的 engines 字段要求 Node.js >=12。版本不符时 npm 默认只给出警告,开启 engine-strict 后会拒绝安装。

ansi-regex 可以商用吗?

ansi-regex 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

ansi-regex 的下载量有多少?

根据 npm 官方下载统计,ansi-regex 在 2026/10/04 – 2026/11/02 这 30 天内被下载 1,995,886,547 次,日均约 66,529,552 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

ansi-regex 还在维护吗?

最新版本 6.5.0 发布于 2026/10/29(6 天前);至今共发布 24 个版本,其中过去 12 个月发布了 3 个;首次发布于 2014/06/04。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。