Tuesday, 27 October 2020

Javascript multiple string replace and regex to remove all ANSI encodings

Note: String.replace returns a new string and the original string will not be changed 

Multiple string replace :

var result = source.replace("foo", "bar").replace("oof", "rab");
https://stackoverflow.com/questions/25245716/remove-all-ansi-colors-styles-from-strings

Regex to remove ANSI encodings from string:
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);
https://stackoverflow.com/questions/25245716/remove-all-ansi-colors-styles-from-strings

Regex testing :
https://regex101.com/

No comments:

Post a Comment