{"id":50730761,"url":"https://github.com/flex-development/ansi-regex","last_synced_at":"2026-06-10T08:31:28.743Z","repository":{"id":325711904,"uuid":"1102110288","full_name":"flex-development/ansi-regex","owner":"flex-development","description":"Regular expression for matching ANSI escape codes","archived":false,"fork":false,"pushed_at":"2026-06-01T23:57:55.000Z","size":3250,"stargazers_count":1,"open_issues_count":8,"forks_count":0,"subscribers_count":0,"default_branch":"main","last_synced_at":"2026-06-02T01:21:26.245Z","etag":null,"topics":["ansi","cli","color","colors","console","node","regex","terminal"],"latest_commit_sha":null,"homepage":"https://github.com/flex-development/ansi-regex","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/flex-development.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":".github/funding.yml","license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null},"funding":{"github":["flex-development"]}},"created_at":"2025-11-22T20:50:45.000Z","updated_at":"2026-05-05T00:44:18.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/flex-development/ansi-regex","commit_stats":null,"previous_names":["flex-development/ansi-regex"],"tags_count":1,"template":false,"template_full_name":null,"purl":"pkg:github/flex-development/ansi-regex","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flex-development%2Fansi-regex","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flex-development%2Fansi-regex/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flex-development%2Fansi-regex/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flex-development%2Fansi-regex/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/flex-development","download_url":"https://codeload.github.com/flex-development/ansi-regex/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/flex-development%2Fansi-regex/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":34144679,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-10T02:00:07.152Z","response_time":89,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["ansi","cli","color","colors","console","node","regex","terminal"],"created_at":"2026-06-10T08:31:26.460Z","updated_at":"2026-06-10T08:31:28.723Z","avatar_url":"https://github.com/flex-development.png","language":"TypeScript","funding_links":["https://github.com/sponsors/flex-development"],"categories":[],"sub_categories":[],"readme":"# ansi-regex\n\n[![ci](https://github.com/flex-development/ansi-regex/actions/workflows/ci.yml/badge.svg?branch=main)](https://github.com/flex-development/ansi-regex/actions/workflows/ci.yml)\n[![github release](https://img.shields.io/github/v/release/flex-development/ansi-regex.svg?include_prereleases\\\u0026sort=semver)](https://github.com/flex-development/ansi-regex/releases/latest)\n[![npm](https://img.shields.io/npm/v/@flex-development/ansi-regex.svg)](https://npmjs.com/package/@flex-development/ansi-regex)\n[![npm downloads](https://img.shields.io/npm/dm/@flex-development/ansi-regex.svg)](https://www.npmcharts.com/compare/@flex-development/ansi-regex?interval=30)\n[![install size](https://packagephobia.now.sh/badge?p=@flex-development/ansi-regex)](https://packagephobia.now.sh/result?p=@flex-development/ansi-regex)\n[![codecov](https://codecov.io/github/flex-development/ansi-regex/graph/badge.svg?token=zIs5SMtGLp)](https://codecov.io/github/flex-development/ansi-regex)\n[![module type: esm](https://img.shields.io/badge/module%20type-esm-brightgreen)](https://github.com/voxpelli/badges-cjs-esm)\n[![license](https://img.shields.io/github/license/flex-development/ansi-regex.svg)](LICENSE.md)\n[![conventional commits](https://img.shields.io/badge/-conventional%20commits-fe5196?logo=conventional-commits\\\u0026logoColor=ffffff)](https://conventionalcommits.org)\n[![typescript](https://img.shields.io/badge/-typescript-3178c6?logo=typescript\\\u0026logoColor=ffffff)](https://typescriptlang.org)\n[![vitest](https://img.shields.io/badge/-vitest-6e9f18?style=flat\\\u0026logo=vitest\\\u0026logoColor=ffffff)](https://vitest.dev)\n[![yarn](https://img.shields.io/badge/-yarn-2c8ebb?style=flat\\\u0026logo=yarn\\\u0026logoColor=ffffff)](https://yarnpkg.com)\n\nRegular expression for matching [ANSI escape codes][ansi-escape-code]\n\n## Contents\n\n- [What is this?](#what-is-this)\n- [Install](#install)\n- [Use](#use)\n- [API](#api)\n  - [`ansiRegex([flags])`](#ansiregexflags)\n  - [`ar`](#ar)\n- [Types](#types)\n  - [`Flags`](#flags)\n- [Contribute](#contribute)\n\n## What is this?\n\nThis is a tiny, but useful package for matching [ANSI escape codes][ansi-escape-code] in strings.\n\n## Install\n\nThis package is [ESM only][esm].\n\nIn Node.js with [yarn][]:\n\n```sh\nyarn add @flex-development/ansi-regex\n```\n\n\u003cblockquote\u003e\n  \u003csmall\u003e\n    See \u003ca href='https://yarnpkg.com/protocol/git'\u003eGit - Protocols | Yarn\u003c/a\u003e\n    \u0026nbsp;for details regarding installing from Git.\n  \u003c/small\u003e\n\u003c/blockquote\u003e\n\nIn Deno with [`esm.sh`][esmsh]:\n\n```ts\nimport { ansiRegex } from 'https://esm.sh/@flex-development/ansi-regex'\n```\n\nIn browsers with [`esm.sh`][esmsh]:\n\n```html\n\u003cscript type=\"module\"\u003e\n  import { ar } from 'https://esm.sh/@flex-development/ansi-regex'\n\u003c/script\u003e\n```\n\nWith [bun][]:\n\n```sh\nbun add @flex-development/ansi-regex\n```\n\n\u003cblockquote\u003e\n  \u003csmall\u003e\n    See \u003ca href='https://bun.com/docs/cli/add'\u003e\u003ccode\u003ebun add\u003c/code\u003e\u003c/a\u003e for more details.\n  \u003c/small\u003e\n\u003c/blockquote\u003e\n\n## Use\n\n[`example.mjs`](./example.mjs):\n\n```js\nimport { ansiRegex } from '@flex-development/ansi-regex'\nimport c from '@flex-development/colors'\nimport { ok } from 'devlop'\n\nconst emojis = '🦄🦾🚀'\nconst hello = c.bgBlue(c.bold('hello world 🌎'))\n\nconsole.log(`${JSON.stringify(emojis)}:`, ansiRegex().test(emojis))\nconsole.log(`${JSON.stringify(hello)}:`, ansiRegex().test(hello))\n\nfor (const match of hello.matchAll(ansiRegex({ d: true }))) {\n  const { groups, index, indices } = match\n\n  ok(groups, 'expected `groups`')\n\n  console.dir({\n    group: Object.entries(groups).reduce((acc, [key, value]) =\u003e {\n      if (typeof value === 'string') value = hrc(value)\n      return acc[key] = value, acc\n    }, {}),\n    index,\n    indices: [...indices]\n  }, { sorted: true })\n}\n\n/**\n * Make control characters in `string` human-readable.\n *\n * @this {void}\n *\n * @param {string} string\n *  The string containing control characters\n * @return {string}\n *  `string` with human-readable control characters\n */\nfunction hrc(string) {\n  /**\n   * Regular expression matching control characters.\n   *\n   * @const {RegExp} controls\n   */\n  const controls = /[\\u0000-\\u001F\\u007F-\\u009F]/g\n\n  return string.replace(controls, hr)\n\n  /**\n   * Convert a control `character` to a human-readable string.\n   *\n   * @this {void}\n   *\n   * @param {string} character\n   *  The control character\n   * @return {string}\n   *  The control `character` as a human-readable string\n   */\n  function hr(character) {\n    return `\\\\u${character.codePointAt(0).toString(16).padStart(4, '0')}`\n  }\n}\n```\n\n...yields\n\n```sh\n\"🦄🦾🚀\": false\n\"\\u001b[44m\\u001b[1mhello world 🌎\\u001b[22m\\u001b[49m\": true\n{\n  group: {\n    ansi: '\\\\u001b[44m',\n    csi: '\\\\u001b[44m',\n    csi_final: 'm',\n    csi_intermediate: '',\n    csi_introducer: '\\\\u001b',\n    csi_params: '44',\n    esc: undefined,\n    esc_final: undefined,\n    osc: undefined,\n    osc_command: undefined,\n    osc_data: undefined,\n    osc_introducer: undefined,\n    osc_sep: undefined,\n    osc_terminator: undefined\n  },\n  index: 0,\n  indices: [\n    [ 0, 5 ],\n    [ 0, 5 ],\n    undefined,\n    undefined,\n    undefined,\n    undefined,\n    undefined,\n    undefined,\n    [ 0, 5 ],\n    [ 0, 1 ],\n    [ 2, 4 ],\n    [ 4, 4 ],\n    [ 4, 5 ],\n    undefined,\n    undefined\n  ]\n}\n{\n  group: {\n    ansi: '\\\\u001b[1m',\n    csi: '\\\\u001b[1m',\n    csi_final: 'm',\n    csi_intermediate: '',\n    csi_introducer: '\\\\u001b',\n    csi_params: '1',\n    esc: undefined,\n    esc_final: undefined,\n    osc: undefined,\n    osc_command: undefined,\n    osc_data: undefined,\n    osc_introducer: undefined,\n    osc_sep: undefined,\n    osc_terminator: undefined\n  },\n  index: 5,\n  indices: [\n    [ 5, 9 ],\n    [ 5, 9 ],\n    undefined,\n    undefined,\n    undefined,\n    undefined,\n    undefined,\n    undefined,\n    [ 5, 9 ],\n    [ 5, 6 ],\n    [ 7, 8 ],\n    [ 8, 8 ],\n    [ 8, 9 ],\n    undefined,\n    undefined\n  ]\n}\n{\n  group: {\n    ansi: '\\\\u001b[22m',\n    csi: '\\\\u001b[22m',\n    csi_final: 'm',\n    csi_intermediate: '',\n    csi_introducer: '\\\\u001b',\n    csi_params: '22',\n    esc: undefined,\n    esc_final: undefined,\n    osc: undefined,\n    osc_command: undefined,\n    osc_data: undefined,\n    osc_introducer: undefined,\n    osc_sep: undefined,\n    osc_terminator: undefined\n  },\n  index: 23,\n  indices: [\n    [ 23, 28 ],\n    [ 23, 28 ],\n    undefined,\n    undefined,\n    undefined,\n    undefined,\n    undefined,\n    undefined,\n    [ 23, 28 ],\n    [ 23, 24 ],\n    [ 25, 27 ],\n    [ 27, 27 ],\n    [ 27, 28 ],\n    undefined,\n    undefined\n  ]\n}\n{\n  group: {\n    ansi: '\\\\u001b[49m',\n    csi: '\\\\u001b[49m',\n    csi_final: 'm',\n    csi_intermediate: '',\n    csi_introducer: '\\\\u001b',\n    csi_params: '49',\n    esc: undefined,\n    esc_final: undefined,\n    osc: undefined,\n    osc_command: undefined,\n    osc_data: undefined,\n    osc_introducer: undefined,\n    osc_sep: undefined,\n    osc_terminator: undefined\n  },\n  index: 28,\n  indices: [\n    [ 28, 33 ],\n    [ 28, 33 ],\n    undefined,\n    undefined,\n    undefined,\n    undefined,\n    undefined,\n    undefined,\n    [ 28, 33 ],\n    [ 28, 29 ],\n    [ 30, 32 ],\n    [ 32, 32 ],\n    [ 32, 33 ],\n    undefined,\n    undefined\n  ]\n}\n```\n\n## API\n\nThis package exports the following identifiers:\n\n- [`ansiRegex`](#ansiregexflags)\n- [`ar`](#ar)\n\nThe default export is [`ar`](#ar).\n\n### `ansiRegex([flags])`\n\nCreate a regular expression matching ANSI escape codes.\n\n#### Parameters\n\n- `flags` ([`Flags`](#flags) | `null` | `undefined`)\n  — an object representing the regular expression flags to apply\n\n#### Returns\n\n(`RegExp`) New regular expression matching ANSI escape codes\n\n### `ar`\n\n(`RegExp`)\n\nThe default regular expression matching ANSI escape codes.\n\n## Types\n\nThis package is fully typed with [TypeScript][].\n\n### `Flags`\n\nRecord, where each key is a regular expression flag\nand each truthy value indicates if the flag should be applied to the regular expression (`interface`).\n\n```ts\ninterface Flags {/* see code */}\n```\n\nWhen developing extensions that use additional flags, augment `Flags` to register custom flags:\n\n```ts\ndeclare module '@flex-development/ansi-regex' {\n  interface Flags {\n    i?: boolean | null | undefined\n  }\n}\n```\n\n#### Properties\n\n- `d?` (`boolean` | `null` | `undefined`)\n  — whether to generate indices for substring matches\n  \u003e 👀 [`RegExp#hasIndices`][regexp-d]\n- `g?` (`boolean` | `null` | `undefined`)\n  — whether to perform global search\n  \u003e 👀 [`RegExp#global`][regexp-g]\n- `u?` (`boolean` | `null` | `undefined`)\n  — whether to treat a pattern as a sequence of unicode code points\n  \u003e 👀 [`RegExp#unicode`][regexp-u]\n- `v?` (`boolean` | `null` | `undefined`)\n  — whether to treat a pattern as a sequence of unicode code points.\\\\\n  \u003e 👉 the `v` flag is an \"upgrade\" to the `u` flag that enables additional unicode-related features.\n  \u003e because `u` and `v` interpret the same regex in incompatible ways,\n  \u003e enabling both flags at once results in a `SyntaxError`\\\n  \u003e 👀 [`RegExp#unicodeSets`][regexp-v]\n- `y?` (`boolean` | `null` | `undefined`)\n  — whether to start matches at the current position in the target string\n  \u003e 👀 [`RegExp#sticky`][regexp-y]\n\n## Contribute\n\nSee [`CONTRIBUTING.md`](CONTRIBUTING.md).\n\nThis project has a [code of conduct](./CODE_OF_CONDUCT.md). By interacting with this repository, organization, or\ncommunity you agree to abide by its terms.\n\n[ansi-escape-code]: https://en.wikipedia.org/wiki/ANSI_escape_code\n\n[bun]: https://bun.sh\n\n[esm]: https://gist.github.com/sindresorhus/a39789f98801d908bbc7ff3ecc99d99c\n\n[esmsh]: https://esm.sh\n\n[regexp-d]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp/hasIndices\n\n[regexp-g]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp/global\n\n[regexp-u]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicode\n\n[regexp-v]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp/unicodeSets\n\n[regexp-y]: https://developer.mozilla.org/docs/Web/JavaScript/Reference/Global_Objects/RegExp/sticky\n\n[typescript]: https://www.typescriptlang.org\n\n[yarn]: https://yarnpkg.com\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflex-development%2Fansi-regex","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fflex-development%2Fansi-regex","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fflex-development%2Fansi-regex/lists"}