I find it difficult to find a problem with the function below. The first parameter is a string containing ANSI color codes, and the second parameter is a logical one.
If the boolean value is set to false , the full text is done in a line.
If the boolean value is set to true , the loop will convert all color codes to something easier for me later.
I suspect RegExp 's problem is that for some reason it gets confused between 1, 33, and 0; 31.
var colorReplace = function( input, replace ) { var replaceColors = { "0;31" : "{r", "1;31" : "{R", "0;32" : "{g", "1;32" : "{G", "0;33" : "{y", "1;33" : "{Y", "0;34" : "{b", "1;34" : "{B", "0;35" : "{m", "1;35" : "{M", "0;36" : "{c", "1;36" : "{C", "0;37" : "{w", "1;37" : "{W", "1;30" : "{*", "0" : "{x" }; if ( replace ) { for( k in replaceColors ) { //console.log( "\033\[" + k + "m" + replaceColors[ k ] ); var re = new RegExp( "\033\[[" + k + "]*m", "g" ); input = input.replace( re, replaceColors[ k ] ); } } else { input = input.replace( /\033\[[0-9;]*m/g, "" ); } return input; }; console.log( "abcd\033[1;32mefgh\033[1;33mijkl\033[0m" ); console.log( colorReplace( "abcd\033[1;32mefgh\033[1;33mijkl", true ) );
Actual conclusion:

Where should it be abcd{Gefgh{Yijkl
Does anyone know what is wrong now?