or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

tessl/npm-strip-ansi

Strip ANSI escape codes from a string

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/strip-ansi@7.1.x

To install, run

npx @tessl/cli install tessl/npm-strip-ansi@7.1.0

0

# Strip ANSI

1

2

Strip ANSI provides a lightweight utility for removing ANSI escape codes from strings. It offers a simple, single-function API that accepts any string and returns a clean version with all ANSI formatting codes (colors, styles, cursor movements, etc.) stripped out, making it essential for processing terminal output and colored text in Node.js applications.

3

4

## Package Information

5

6

- **Package Name**: strip-ansi

7

- **Package Type**: npm

8

- **Language**: JavaScript (ES modules) with TypeScript definitions

9

- **Installation**: `npm install strip-ansi`

10

11

## Core Imports

12

13

```javascript

14

import stripAnsi from 'strip-ansi';

15

```

16

17

## Basic Usage

18

19

```javascript

20

import stripAnsi from 'strip-ansi';

21

22

// Remove ANSI codes from colored text

23

stripAnsi('\u001B[4mUnicorn\u001B[0m');

24

//=> 'Unicorn'

25

26

// Remove ANSI codes from terminal links

27

stripAnsi('\u001B]8;;https://github.com\u0007Click\u001B]8;;\u0007');

28

//=> 'Click'

29

30

// Process complex terminal output

31

const terminalOutput = '\u001B[0m\u001B[4m\u001B[42m\u001B[31mfoo\u001B[39m\u001B[49m\u001B[24mfoo\u001B[0m';

32

const cleanText = stripAnsi(terminalOutput);

33

//=> 'foofoo'

34

```

35

36

## Capabilities

37

38

### Strip ANSI Function

39

40

Removes all ANSI escape codes from a string while preserving the original text content.

41

42

```javascript { .api }

43

/**

44

* Strip ANSI escape codes from a string

45

* @param string - The input string containing ANSI escape codes to be stripped

46

* @returns The input string with all ANSI escape codes removed

47

* @throws TypeError when input is not a string type

48

*/

49

export default function stripAnsi(string: string): string;

50

```

51

52

**Parameters:**

53

- `string` (string, required): The input string containing ANSI escape codes to be stripped

54

55

**Returns:**

56

- `string`: The input string with all ANSI escape codes removed

57

58

**Error Handling:**

59

- **TypeError**: Thrown when input is not a string type

60

- **Error Message Format**: `Expected a \`string\`, got \`${typeof input}\``

61

62

**Supported ANSI Codes:**

63

- Color codes (foreground and background)

64

- Text styling (bold, italic, underline, etc.)

65

- Cursor movement and positioning codes

66

- Terminal link codes (`\u001B]8;;\u0007`)

67

- Reset and clearing codes

68

- Extended color codes (256-color and RGB)

69

70

**Usage Examples:**

71

72

```javascript

73

import stripAnsi from 'strip-ansi';

74

75

// Basic color removal

76

stripAnsi('\u001B[31mRed text\u001B[0m');

77

//=> 'Red text'

78

79

// Complex styling removal

80

stripAnsi('\u001B[0;33;49;3;9;4mbar\u001B[0m');

81

//=> 'bar'

82

83

// Terminal link removal

84

stripAnsi('\u001B]8;;https://example.com\u0007Link text\u001B]8;;\u0007');

85

//=> 'Link text'

86

87

// Input validation

88

try {

89

stripAnsi(123);

90

} catch (error) {

91

console.log(error.message);

92

//=> 'Expected a `string`, got `number`'

93

}

94

95

// Common use cases

96

const logOutput = '\u001B[32m[INFO]\u001B[0m \u001B[1mOperation completed\u001B[0m';

97

const cleanLog = stripAnsi(logOutput);

98

//=> '[INFO] Operation completed'

99

100

// Processing command line output

101

const cliOutput = '\u001B[00;38;5;244m\u001B[m\u001B[00;38;5;33mfilename.js\u001B[0m';

102

const filename = stripAnsi(cliOutput);

103

//=> 'filename.js'

104

```

105

106

**Implementation Details:**

107

- Uses the `ansi-regex` library for comprehensive ANSI code pattern matching

108

- Globally replaces all matched ANSI codes in a single pass

109

- Optimized for performance with automatic regex lastIndex management

110

- Handles all standard ANSI escape sequence formats

111

- Type-safe with built-in input validation