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...
couleurs
and for unstyling I useansi-parser
. Hope it's now clearer.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).chalk
is only for coloring. From what I see,strip-ansi
is used for this, and you are one of the contributors. :-) Nice!strip-ansi
package. I always liked tiny and cute npm packages. :)