I use a library that adds ANSI colors / styles to strings. For example:
> "Hello World".rgb(255, 255, 255)
'u001b[38;5;231mHello Worldu001b[0m'
> "Hello World".rgb(255, 255, 255).bold()
'u001b[1mu001b[38;5;231mHello Worldu001b[0mu001b[22m'
When I do:
console.log('u001b[1mu001b[38;5;231mHello Worldu001b[0mu001b[22m')
a "Hello World"
white and bold message will be output.
Having a string like 'u001b[1mu001b[38;5;231mHello Worldu001b[0mu001b[22m'
how can these elements be removed?
foo('u001b[1mu001b[38;5;231mHello Worldu001b[0mu001b[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[1mu001b[38;5;231mHello Worldu001b[0mu001b[22m')""
, function (err, stderr, stdout) { console.log(stdout);
});
But the output is the same...
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…