从字符串中删除所有ANSI颜色/样式

时间:2021-06-28 00:06:20

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

我使用一个库,为字符串添加ANSI颜色/样式。例如:

> "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.

将输出“Hello World”白色和粗体消息。

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

有一个字符串,如'\ u001b [1m \ u001b [38; 5; 231mHello World \ u001b [0m \ u001b [22m'如何删除这些元素?

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...

但输出是一样的......

2 个解决方案

#1


27  

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.

这匹配大多数ANSI转义码,不仅仅是颜色,包括扩展的VT100代码,古老的/专有的打印机代码等。

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.

请注意,上述正则表达式中的\ u001b可能不适用于您的特定库(即使它应该);如果没有,请查看关于可接受的转义字符的类似问题的答案。

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

如果你不喜欢正则表达式,你可以随时使用strip-ansi包。


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.

例如,下面的字符串jumpUpAndRed包含用于跳转到上一行的ANSI代码,写入一些红色文本,然后返回到下一行的开头 - 其中需要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);

#2


5  

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的顺序是样式。你只需要删除它。因此,使用以下模式替换全局:

/\u001b\[.*?m/g

Thus,

从而,

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

#1


27  

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.

这匹配大多数ANSI转义码,不仅仅是颜色,包括扩展的VT100代码,古老的/专有的打印机代码等。

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.

请注意,上述正则表达式中的\ u001b可能不适用于您的特定库(即使它应该);如果没有,请查看关于可接受的转义字符的类似问题的答案。

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

如果你不喜欢正则表达式,你可以随时使用strip-ansi包。


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.

例如,下面的字符串jumpUpAndRed包含用于跳转到上一行的ANSI代码,写入一些红色文本,然后返回到下一行的开头 - 其中需要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);

#2


5  

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的顺序是样式。你只需要删除它。因此,使用以下模式替换全局:

/\u001b\[.*?m/g

Thus,

从而,

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