npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@flex-development/ansi-regex

v1.0.0

Published

Regular expression for matching ANSI escape codes

Readme

ansi-regex

ci github release npm npm downloads install size codecov module type: esm license conventional commits typescript vitest yarn

Regular expression for matching ANSI escape codes

Contents

What is this?

This is a tiny, but useful package for matching ANSI escape codes in strings.

Install

This package is ESM only.

In Node.js with yarn:

yarn add @flex-development/ansi-regex

In Deno with esm.sh:

import { ansiRegex } from 'https://esm.sh/@flex-development/ansi-regex'

In browsers with esm.sh:

<script type="module">
  import { ar } from 'https://esm.sh/@flex-development/ansi-regex'
</script>

With bun:

bun add @flex-development/ansi-regex

Use

example.mjs:

import { ansiRegex } from '@flex-development/ansi-regex'
import c from '@flex-development/colors'
import { ok } from 'devlop'

const emojis = '🦄🦾🚀'
const hello = c.bgBlue(c.bold('hello world 🌎'))

console.log(`${JSON.stringify(emojis)}:`, ansiRegex().test(emojis))
console.log(`${JSON.stringify(hello)}:`, ansiRegex().test(hello))

for (const match of hello.matchAll(ansiRegex({ d: true }))) {
  const { groups, index, indices } = match

  ok(groups, 'expected `groups`')

  console.dir({
    group: Object.entries(groups).reduce((acc, [key, value]) => {
      if (typeof value === 'string') value = hrc(value)
      return acc[key] = value, acc
    }, {}),
    index,
    indices: [...indices]
  }, { sorted: true })
}

/**
 * Make control characters in `string` human-readable.
 *
 * @this {void}
 *
 * @param {string} string
 *  The string containing control characters
 * @return {string}
 *  `string` with human-readable control characters
 */
function hrc(string) {
  /**
   * Regular expression matching control characters.
   *
   * @const {RegExp} controls
   */
  const controls = /[\u0000-\u001F\u007F-\u009F]/g

  return string.replace(controls, hr)

  /**
   * Convert a control `character` to a human-readable string.
   *
   * @this {void}
   *
   * @param {string} character
   *  The control character
   * @return {string}
   *  The control `character` as a human-readable string
   */
  function hr(character) {
    return `\\u${character.codePointAt(0).toString(16).padStart(4, '0')}`
  }
}

...yields

"🦄🦾🚀": false
"\u001b[44m\u001b[1mhello world 🌎\u001b[22m\u001b[49m": true
{
  group: {
    ansi: '\\u001b[44m',
    csi: '\\u001b[44m',
    csi_final: 'm',
    csi_intermediate: '',
    csi_introducer: '\\u001b',
    csi_params: '44',
    esc: undefined,
    esc_final: undefined,
    osc: undefined,
    osc_command: undefined,
    osc_data: undefined,
    osc_introducer: undefined,
    osc_sep: undefined,
    osc_terminator: undefined
  },
  index: 0,
  indices: [
    [ 0, 5 ],
    [ 0, 5 ],
    undefined,
    undefined,
    undefined,
    undefined,
    undefined,
    undefined,
    [ 0, 5 ],
    [ 0, 1 ],
    [ 2, 4 ],
    [ 4, 4 ],
    [ 4, 5 ],
    undefined,
    undefined
  ]
}
{
  group: {
    ansi: '\\u001b[1m',
    csi: '\\u001b[1m',
    csi_final: 'm',
    csi_intermediate: '',
    csi_introducer: '\\u001b',
    csi_params: '1',
    esc: undefined,
    esc_final: undefined,
    osc: undefined,
    osc_command: undefined,
    osc_data: undefined,
    osc_introducer: undefined,
    osc_sep: undefined,
    osc_terminator: undefined
  },
  index: 5,
  indices: [
    [ 5, 9 ],
    [ 5, 9 ],
    undefined,
    undefined,
    undefined,
    undefined,
    undefined,
    undefined,
    [ 5, 9 ],
    [ 5, 6 ],
    [ 7, 8 ],
    [ 8, 8 ],
    [ 8, 9 ],
    undefined,
    undefined
  ]
}
{
  group: {
    ansi: '\\u001b[22m',
    csi: '\\u001b[22m',
    csi_final: 'm',
    csi_intermediate: '',
    csi_introducer: '\\u001b',
    csi_params: '22',
    esc: undefined,
    esc_final: undefined,
    osc: undefined,
    osc_command: undefined,
    osc_data: undefined,
    osc_introducer: undefined,
    osc_sep: undefined,
    osc_terminator: undefined
  },
  index: 23,
  indices: [
    [ 23, 28 ],
    [ 23, 28 ],
    undefined,
    undefined,
    undefined,
    undefined,
    undefined,
    undefined,
    [ 23, 28 ],
    [ 23, 24 ],
    [ 25, 27 ],
    [ 27, 27 ],
    [ 27, 28 ],
    undefined,
    undefined
  ]
}
{
  group: {
    ansi: '\\u001b[49m',
    csi: '\\u001b[49m',
    csi_final: 'm',
    csi_intermediate: '',
    csi_introducer: '\\u001b',
    csi_params: '49',
    esc: undefined,
    esc_final: undefined,
    osc: undefined,
    osc_command: undefined,
    osc_data: undefined,
    osc_introducer: undefined,
    osc_sep: undefined,
    osc_terminator: undefined
  },
  index: 28,
  indices: [
    [ 28, 33 ],
    [ 28, 33 ],
    undefined,
    undefined,
    undefined,
    undefined,
    undefined,
    undefined,
    [ 28, 33 ],
    [ 28, 29 ],
    [ 30, 32 ],
    [ 32, 32 ],
    [ 32, 33 ],
    undefined,
    undefined
  ]
}

API

This package exports the following identifiers:

The default export is ar.

ansiRegex([flags])

Create a regular expression matching ANSI escape codes.

Parameters

  • flags (Flags | null | undefined) — an object representing the regular expression flags to apply

Returns

(RegExp) New regular expression matching ANSI escape codes

ar

(RegExp)

The default regular expression matching ANSI escape codes.

Types

This package is fully typed with TypeScript.

Flags

Record, where each key is a regular expression flag and each truthy value indicates if the flag should be applied to the regular expression (interface).

interface Flags {/* see code */}

When developing extensions that use additional flags, augment Flags to register custom flags:

declare module '@flex-development/ansi-regex' {
  interface Flags {
    i?: boolean | null | undefined
  }
}

Properties

  • d? (boolean | null | undefined) — whether to generate indices for substring matches

    👀 RegExp#hasIndices

  • g? (boolean | null | undefined) — whether to perform global search

    👀 RegExp#global

  • u? (boolean | null | undefined) — whether to treat a pattern as a sequence of unicode code points

    👀 RegExp#unicode

  • v? (boolean | null | undefined) — whether to treat a pattern as a sequence of unicode code points.\

    👉 the v flag is an "upgrade" to the u flag that enables additional unicode-related features. because u and v interpret the same regex in incompatible ways, enabling both flags at once results in a SyntaxError
    👀 RegExp#unicodeSets

  • y? (boolean | null | undefined) — whether to start matches at the current position in the target string

    👀 RegExp#sticky

Contribute

See CONTRIBUTING.md.

This project has a code of conduct. By interacting with this repository, organization, or community you agree to abide by its terms.