1 | # ansi-regex
|
---|
2 |
|
---|
3 | > Regular expression for matching [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code)
|
---|
4 |
|
---|
5 |
|
---|
6 | ## Install
|
---|
7 |
|
---|
8 | ```
|
---|
9 | $ npm install ansi-regex
|
---|
10 | ```
|
---|
11 |
|
---|
12 |
|
---|
13 | ## Usage
|
---|
14 |
|
---|
15 | ```js
|
---|
16 | const ansiRegex = require('ansi-regex');
|
---|
17 |
|
---|
18 | ansiRegex().test('\u001B[4mcake\u001B[0m');
|
---|
19 | //=> true
|
---|
20 |
|
---|
21 | ansiRegex().test('cake');
|
---|
22 | //=> false
|
---|
23 |
|
---|
24 | '\u001B[4mcake\u001B[0m'.match(ansiRegex());
|
---|
25 | //=> ['\u001B[4m', '\u001B[0m']
|
---|
26 |
|
---|
27 | '\u001B[4mcake\u001B[0m'.match(ansiRegex({onlyFirst: true}));
|
---|
28 | //=> ['\u001B[4m']
|
---|
29 |
|
---|
30 | '\u001B]8;;https://github.com\u0007click\u001B]8;;\u0007'.match(ansiRegex());
|
---|
31 | //=> ['\u001B]8;;https://github.com\u0007', '\u001B]8;;\u0007']
|
---|
32 | ```
|
---|
33 |
|
---|
34 |
|
---|
35 | ## API
|
---|
36 |
|
---|
37 | ### ansiRegex(options?)
|
---|
38 |
|
---|
39 | Returns a regex for matching ANSI escape codes.
|
---|
40 |
|
---|
41 | #### options
|
---|
42 |
|
---|
43 | Type: `object`
|
---|
44 |
|
---|
45 | ##### onlyFirst
|
---|
46 |
|
---|
47 | Type: `boolean`<br>
|
---|
48 | Default: `false` *(Matches any ANSI escape codes in a string)*
|
---|
49 |
|
---|
50 | Match only the first ANSI escape.
|
---|
51 |
|
---|
52 |
|
---|
53 | ## FAQ
|
---|
54 |
|
---|
55 | ### Why do you test for codes not in the ECMA 48 standard?
|
---|
56 |
|
---|
57 | 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.
|
---|
58 |
|
---|
59 | 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.
|
---|
60 |
|
---|
61 |
|
---|
62 | ## Maintainers
|
---|
63 |
|
---|
64 | - [Sindre Sorhus](https://github.com/sindresorhus)
|
---|
65 | - [Josh Junon](https://github.com/qix-)
|
---|
66 |
|
---|
67 |
|
---|
68 | ---
|
---|
69 |
|
---|
70 | <div align="center">
|
---|
71 | <b>
|
---|
72 | <a href="https://tidelift.com/subscription/pkg/npm-ansi-regex?utm_source=npm-ansi-regex&utm_medium=referral&utm_campaign=readme">Get professional support for this package with a Tidelift subscription</a>
|
---|
73 | </b>
|
---|
74 | <br>
|
---|
75 | <sub>
|
---|
76 | Tidelift helps make open source sustainable for maintainers while giving companies<br>assurances about security, maintenance, and licensing for their dependencies.
|
---|
77 | </sub>
|
---|
78 | </div>
|
---|