Cyb*_*rix 6 javascript regex string ansi
我无法通过以下功能找到问题.第一个参数是包含ANSI颜色代码的字符串,第二个参数是布尔值.
如果布尔值设置为false
,则对字符串进行完全删除.
如果布尔值设置为true
,则循环将每个颜色代码转换为更容易让我稍后解析的内容.
我怀疑RegExp
是问题,因为它在1; 33和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 ) );
Run Code Online (Sandbox Code Playgroud)
实际输出是:
它应该在哪里 abcd{Gefgh{Yijkl
谁知道现在有什么问题?
您可以在字符串和regexp中使用八进制代码
x = "\033[1mHello Bold World!\033[0m\n";
x = x.replace(/\033\[[0-9;]*m/,"");
print(x);
Run Code Online (Sandbox Code Playgroud)