43

I use a library that adds ANSI colors / styles to strings. For example:

> "Hello World".rgb(255, 255, 255)
'\u001b[38;5;231mHello World\u001b[0m'
> "Hello World".rgb(255, 255, 255).bold()
'\u001b[1m\u001b[38;5;231mHello World\u001b[0m\u001b[22m'

When I do:

console.log('\u001b[1m\u001b[38;5;231mHello World\u001b[0m\u001b[22m')

a "Hello World" white and bold message will be output.

Having a string like '\u001b[1m\u001b[38;5;231mHello World\u001b[0m\u001b[22m' how can these elements be removed?

foo('\u001b[1m\u001b[38;5;231mHello World\u001b[0m\u001b[22m') //=> "Hello World"

Maybe a good regular expression? Or is there any built-in feature?


The work around I was thinking was to create child process:

require("child_process")
 .exec("node -pe \"console.error('\u001b[1m\u001b[38;5;231mHello World\u001b[0m\u001b[22m')\""
 , function (err, stderr, stdout) { console.log(stdout);
 });

But the output is the same...

6
  • You should use chalk. It checks for colors being enabled or not and will automatically strip ANSI codes from your text. Jun 30, 2015 at 20:21
  • @Qix It's not about styling, it's about unstyling -- removing the ANSI styles from strings. For coloring, I use couleurs and for unstyling I use ansi-parser. Hope it's now clearer. Jul 1, 2015 at 6:47
  • I use a library that adds ANSI colors / styles to strings And I'm saying by using chalk, you can remove those codes automatically if you so choose (i.e. you can style and automatically remove). Jul 1, 2015 at 10:36
  • 1
    @Qix Ah, I've got it. I thought chalk is only for coloring. From what I see, strip-ansi is used for this, and you are one of the contributors. :-) Nice! Jul 1, 2015 at 13:45
  • 1
    @Qix You could update your answer including the strip-ansi package. I always liked tiny and cute npm packages. :) Jul 1, 2015 at 13:56

3 Answers 3

98

The regex you should be using is

/[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g

This matches most of the ANSI escape codes, beyond just colors, including the extended VT100 codes, archaic/proprietary printer codes, etc.

Note that the \u001b in the above regex may not work for your particular library (even though it should); check out my answer to a similar question regarding acceptable escape characters if it doesn't.

If you don't like regexes, you can always use the strip-ansi package.


For instance, the string jumpUpAndRed below contains ANSI codes for jumping to the previous line, writing some red text, and then going back to the beginning of the next line - of which require suffixes other than m.

var jumpUpAndRed = "\x1b[F\x1b[31;1mHello, there!\x1b[m\x1b[E";
var justText = jumpUpAndRed.replace(
    /[\u001b\u009b][[()#;?]*(?:[0-9]{1,4}(?:;[0-9]{0,4})*)?[0-9A-ORZcf-nqry=><]/g, '');
console.log(justText);

4
  • 5
    Full Disclosure: I'm a maintainer of the chalk projects mentioned above Jul 1, 2015 at 13:58
  • 2
    This is amazing. You are all amazing
    – SeanMC
    Jan 29, 2022 at 5:46
  • I'd vote for stripcolorcodes lib.
    – AntonK
    Jan 4 at 13:13
  • It hasn't been updated in 11 years. Further the better approach is simply not to emit control codes at all unless the output is probably a TTY. Jan 4 at 17:12
15

The escape character is \u001b, and the sequence from [ until first m is encountered is the styling. You just need to remove that. So, replace globally using the following pattern:

/\u001b\[.*?m/g

Thus,

'\u001b[1m\u001b[38;5;231mHello World\u001b[0m\u001b[22m'.replace(/\u001b\[.*?m/g, '')
0
4

The colors are like ESC[39m format, the shortest regexp is for it the /\u001b[^m]*?m/g

Where \u001b is the ESC character, [^m]*? is any character(s) till m (not greedy pattern), the m itself, and /g for global (all) replace.

Example:

    var line="\x1B[90m2021-02-03 09:35:50.323\x1B[39m\t\x1B[97mFinding: \x1B[39m\x1B[97m»\x1B[39m\x1B[33m42125121242\x1B[39m\x1B[97m«\x1B[39m\x1B[0m\x1B[0m\t\x1B[92mOK\x1B[39m";
    
    console.log(line.replace(/\u001b[^m]*?m/g,""));
// ->  2021-02-03 09:35:50.323 Finding: »42125121242«  OK ( without colors )
    console.log(line);
// -> 2021-02-03 09:35:50.323 Finding: »42125121242«  OK ( colored )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.