ansi-regex

license: MIT

test: mocha

type: git

description: Regular expression for matching ANSI escape codes

keywords: ansi styles color colour colors terminal console cli string tty escape formatting rgb 256 shell xterm command-line text regex regexp re match test find pattern

test: mocha test/test.js

view-supported: node test/viewCodes.js

test: xo && ava --verbose

view-supported: node fixtures/view-codes.js

test: xo && ava

npm-signature:
-----BEGIN PGP SIGNATURE-----
Version: OpenPGP.js v3.0.4
Comment: https://openpgpjs.org

wsFcBAEBCAAQBQJboLTyCRA9TVsSAnZWagAAGnQP/ikFm/pU0nz/k+kwLXQF
QaOqkrWmoNlb6fiz7NElGRxe+a7K5dkovim/Plf66V+kcT1u47O/IsNn+1AU
UYztxJ2Syn8QWdLgCKZlVy4jgN8qrq3ACLHYV6XFo+fV7JVoRqZnVLZlJtCg
xaWT3K+ckyo0SbFrSKHchZWSha20mtgBjUzeVnzsx0zUELhL3I9soTTMaXOp
8tYKs2rxhYduYewTu5hDfi/aAYlWhUuS0KkxZ+lR8zjrRJV3OMMcexJ+6qzZ
5YozFB5L72r+aXJ5b9epDbRMTUh9D+J6OIRED4COBTbi5VaxjE5Qef7Y4wuy
B5t+S8shSu/yPbMGKdHW+lBIv8tskZgCislmedHL7N3dWqKRqaelIKJHp8ea
5NlvT4Bq6bIgggoPSG4ccVPvnwhPzZso3yBhx1Xzxdysb3nRor0ZrkDr9NSS
PHhQwxlIZr3Ba4R7BWeyNjvOw4kAB8EicYP9REruWDoMxee/mWYd3xo1rOTD
GocidueWQwHJPbyxGm4boKcBsToDN7boOoZAJONR0DBFgB5fCFB7FbzLRgil
h3sOpoe7K+8/tCC/GlbpnhPEtNtFAWmtVEr5fuG6pwCxwO9ZOUNsFKCccs/Q
SkXo9JmCGUgLzW04Z0Cms6BMXuS/VKERq+QyJKq5EQXJ93KwEBWj7gSmuYar
miIW
=6Av/
-----END PGP SIGNATURE-----

npm-signature:
-----BEGIN PGP SIGNATURE-----
Version: OpenPGP.js v3.0.4
Comment: https://openpgpjs.org

wsFcBAEBCAAQBQJcg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2EEOlAKzxkxTKCAX3+7Re2H9PVAMmtCXWj09qwaxwRxtVc
CHi7FGRefk3a3FJxdLPKHdmOR7fQJy5TRoOhutjhQPFZKGqFhReCtsZwHGuJ
KNqagC1KBLFQRLLwtJGhdQGpLUYZgcurWC1OX+wbMBJB8ZrC0HBIuUAlLCOJ
oKhRI81dJ89lT75+xe6B2X8TZsWzQNuohd4V5ymKBlztU3wOPk1r+SEt8tFu
H+iesokrKb/tSHL0cr5XVKq5yJC3oWFpvdfM7xToVS4ehxfh50Ib8OuhFJXr
xws6yKp+PA4RTy533CHyfME1K2lM8M8YQPnFs6uHeaWMQhJHxTsYWIccZP+t
17qqhl64fW6UhO0Nxio2Jpbwrdo1pfmdsN04AVow6nsEfRfGrZ/9Qzq23c0G
awgI4nagjuIuQeknNymHC8IbhpYEa5eRY7+uvElE1bWZwmJshf9ZItStOjYI
1B0E
=DWyA
-----END PGP SIGNATURE-----

test: xo && ava && tsd

npm-signature:
-----BEGIN PGP SIGNATURE-----
Version: OpenPGP.js v3.0.13
Comment: https://openpgpjs.org

wsFcBAEBCAAQBQJgeShnCRA9TVsSAnZWagAA5skP/RJzYexBxMl+ln5Waq+j
JcMY3Gjz1UL+qodDm2QbAMK41c92SEVh5f26fdyRUfhSgLo3Jjn8PBcMlStr
jAH/FBhyrQsFGyxVsgOhOfkSxDjQtWRX9JBCwnYbCp8zfpOcCqIGgwl6Za8X
jQkODSEkwpBdIAI0I8E8bTLcfO/RK7pPfbEWptrdNJgsv80Y3ye8fLBiFERm
Hzgt1FCR5m0LyVlfq941MpmLyzd0f0dewP3WiQ9yDyD60Sune4s6jw4n97pU
DG3H/MboqIaGkcQPyVWTtM92UTRKt6dBcIfnCAXgf6qD7Iu2/hWUQQg6NpVX
AD22nWt5e6GCJXOzDDQSY5xO5dnR4DcYpcQBiszYqxRopGTSWy5E6pK6x+5N
a3mbFq4zFi33TtjM8MRdwGRyIqnGwsYuSTCw/ie6EHZNBlBMLhcMfffYYwtD
8Yc04q1n3jsloKT4Z3TGDkTDPALEQk7LcOklArsOHkqVDjtUyPMdm3y8ew73
4k9b7G/x4AnP9yQ6zhIASoToZ8YLnQCPZxFZ93HnMAhPAvwe9n6Ni6LqnKzU
5myBJYckIV6qcOp1nPwxyBTwdv0uXE5h4FLCbE2B3JVe+kV5cbu3OC2rGwN0
ncdZvUwFGo5rNKkHm+jPcI7/BrZGR1+4r/Z8GFrPYGpY20gFJsL8055NsGvu
+l/+
=ys2B
-----END PGP SIGNATURE-----

type: module

npm-signature:
-----BEGIN PGP SIGNATURE-----
Version: OpenPGP.js v3.0.13
Comment: https://openpgpjs.org

wsFcBAEBCAAQBQJhO78hCRA9TVsSAnZWagAA+F4P/0Xt4D0GoXduv+o147iO
j3b1oPSNypnmThhmCAUInwB1Xd9SXFEjzuNDNGX/8xeWUQrJssJ+SBEzb5te
r34P6wPHUeuTa2p1j3ABN9ABP/NPCPkaEDgjx86ImkT++3VI8yNEuSSOsghI
5Ssvhm6bLJBWzCt/cweIG7x4ZJQPeUbyoWIJmBobCDyNuVgabmk9B2B1RyO0
f2aounmh5w1fBWsx65MjTi7fu4e/GOP+xscf9Rn1AdRP6rcxJakPjaGTF3HE
az6UNP9UZCuZ4rb6xOFEfLmiqfNbK31NKNLPrABDXF6NkbJepsUhNmju32cg
Uuo4lSkxfFaVyFXFB1h62RmDdemxZWl9j24fVt+99K4BH6xOiwPmOHHHPJnL
XNuZ6eCwdcLjPe8Af/0GEsFqdDu591Hwi2Nl8UNAD5hCzcDTITOTL95Dopaw
GzySTClnDsh17Ud73FWM51FQzVDORa2vOmVzQts3aHlF93ufk9RHtlN9YxAV
UvM3IxMHx2O8Rc2gSc6t/pJAdFACBLkLDGtcQVhxaFcTpGXrKUr+KN2gFC4D
XZA/MvK0YXBmrwec20k2wTAdmi7XO5k8HciCPT7MZDJONTBlt9cC/LL41JNc
vxj0A0loP9R44eVoAJOSbxhpnteeIjw4rwkOySZOqqEsUw5MDFp/peiojlcv
COQk
=VRgZ
-----END PGP SIGNATURE-----

npm-signature:
-----BEGIN PGP SIGNATURE-----
Version: OpenPGP.js v3.0.13
Comment: https://openpgpjs.org

wsFcBAEBCAAQBQJhQMXnCRA9TVsSAnZWagAADYYP/iz1/gbU0N9EjGpKc+Gm
nX7qe5zf8TvcOpxBMDSFiCD5ntg/EH4YguqeUhRgW7X/nx58basGjNU2Payd
6gIBVMEriQUWhS6MllSbiIbxY3ALKRcNhIphqER4kneujLwTqgAMOSjjnOk+
vhPheetSO9ydRVZvb1UHC45D/4Bl0BJjPuAgF6YJlAae+bon50yImBeBUus4
SmcbaBKSoCKT+ml4pfNidfUIJn+Zo8u0u8WI/6hfJ5oD9/eerQaSK/Fjt1hZ
j67NmXWMsMugedkgpKbK3lZX+LucgTrb7L9QJC++LQj6FSRDQBguiODgJ912
VxqjJN0SSqcpNRVk0dTHhI2IoOn4Mu3HVZolsrVFGXsQDMlNHWxehHUtzZ/h
e592hb0tqcyIeMFLUMcRhcnaQZrNKTYv7XSMpfPWh73slOk5AaZFOyY/yZ2k
Iqfm7sKVsijHzRgyveZ+n1QyV6CWFMzQbxnj34t7NpnX429VY/F/ZL/E1Yfu
oJhSbp/K0+k8Gp9g9YNDANE7s5jM8pJzlSOo3XUWyaFaujRHMsAtxgtYikZB
3uzZaYQxryjCkvuvZJFwUBMeLWJP2JKt7bpK3mv7DHvEWTxicBHSPaePhA01
OfiQ7YzPY3pNI/Fki/jloyJgSazLpvlWv7XWAF/OhvB5G3KQbfDe/hbQg0Yq
sUn3
=I35d
-----END PGP SIGNATURE-----

npm-signature:
-----BEGIN PGP SIGNATURE-----
Version: OpenPGP.js v4.10.10
Comment: https://openpgpjs.org

wsFzBAEBCAAGBQJiLA7KACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b
EgJ2Vmq3gQ//a+k/IMT0gBGfY6uA0Cgu+o/GhIow5TQbqkJER9T27TO8Po7h
/07GR3KJTtxgFn+wkRlNLgXxdCRRjXe5df4eHzbMM52Ag49iqrYecm3xC28i
CJArVdmZ0djATnvThwWImD/dHN5cj76MHQzJjWEdWPXXim/cRHHHcHXkWEsv
N64mWjz06J8NqidAS0ARvYGc6QmUeWENzuhUO7Pa2rymBrkccTQkYNJUoTCf
J+DAtQZpUm9jFHOKoKsNtyBkb3BlPLZVjnfF6TNv0Ya2KiW5N3FIQEeS8h54
M9l82ED8sopqO7Z6Vsps2W7xdCTRvKPht+cZJhet4hmcxMQE/lNtH6R4C1hR
0MzhnSYwvvsDfqDbjNaerMe3/yq/EkUj8DTDqnNBPeODWlLgG3WF3tTlA7aQ
IqGk8bVfd8Fxr6EJ4JKjn4bUCbmTSzqK7lAiDX/pDXMOONiDvRwQaDJbT5y5
ycep5aL9mlYGkSOf2dPRp7B71KHKQAmGTl3OupYMrWxSVNAoIk3T4r5j6IX4
+8LlAtusnCJ4mGWo2M4rXhQ1o+5gWyLgyf470hdCkgH24Yp/8348NEMRZD+z
Gv3sPclHhjVZzS0GhEIvHUYYGUuSRxM6Vd6g1lgv8e9LI3/yqvf830l/FHQo
JwEpTS3Q8YrM9t2YpodilybqqpuTbu8uoB4=
=Hgo9
-----END PGP SIGNATURE-----

npm-signature:
-----BEGIN PGP SIGNATURE-----
Version: OpenPGP.js v4.10.10
Comment: https://openpgpjs.org

wsFzBAEBCAAGBQJiQGbIACEJED1NWxICdlZqFiEECWMYAoorWMhJKdjhPU1b
EgJ2Vmqy/Q//Vh9oKpgDxgXcW+UYZ9DZKYScVldnKzuT5VNYLrGT8mMMPxAC
ws4wVRyeWo8WX4jwQqpZaDxr5WlH+X2tbxM+PmwncwkOYTtbTGutmr+cwc16
XaKNwQZt4F5fA32N39Jxyu05sLqje517n3rRFvN6HqzOw21DYIL6N7UpUXeD
FiwNMFOi2S3RMUJgf6EDVNB59MNX7X6W85cvJrjRK8xfUJB50ODx1WfUNG1X
c96t6swskGBdq0riRxtmUTyJgxkgaL4eHnYMpLSad1Xx5rVyHckL9CFajYPo
p64kmi2+UHAswdfzDYuQTuIt2vKzpKCGGP74FSZ5trTsQNuljvjhemFePlAw
XOoq2sf/yTGiNu9W+ved2NPpR/nSQO9UnBp7P/pKcfY92rPOwRc3ypBx4JDC
QHANx8/fM7TtRrVUyVJoyEYzMnDxDQnzeGRrYfSneg1oI7oci62HXmgrfxvh
8TPDgsKALJixmkDc6FQgENjmMrblrmU7ZuK2H1meidi+62MJEoTj5lwXKp9d
hay1RKGB4qKHEO5PVIBrYdvkoZIaUyIrN4KgcqHHjrOKWhcWAZDu3A9wwyLD
AOMLQbPqJccQj2FrSMOGrc1ego70swRFGX21xLW0xqr8p65PTLGBKp0+OgC9
lcz8F/xfg1PkqVRZw4TGuEcBE3EHcDFSrr0=
=xF18
-----END PGP SIGNATURE-----

readme:
# ansi-regex

> Regular expression for matching [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code)

## Install

```sh
npm install ansi-regex
```

## Usage

```js
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](https://github.com/sindresorhus/super-regex).

**I do not consider [ReDoS](https://blog.yossarian.net/2022/12/28/ReDoS-vulnerabilities-and-misaligned-incentives) 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

- [Sindre Sorhus](https://github.com/sindresorhus)
- [Josh Junon](https://github.com/qix-)