gpt4 book ai didi

javascript - 从输入字符串中获取包含字符及其 ANSI 颜色的数组

转载 作者:行者123 更新时间:2023-11-30 05:31:00 27 4
gpt4 key购买 nike

此正则表达式从字符串中删除 ANSI 颜色:/\u001b\[.*?m/g:

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

我们如何提取这样的对象数组:

[
{
start: "\u00\u001b[1m\u001b[38;5;231",
end: "\u001b[0m\u001b[22m",
content: "H"
},
{
start: "\u00\u001b[1m\u001b[38;5;231",
content: "e"
end: "\u001b[0m\u001b[22m",
},
...
]

实现此目的的最佳方法是什么?

最佳答案

What's the optimal way to to this?

可能不使用正则表达式,我在构建它时使我的开发工具崩溃了好几次,但是现在开始吧:

>>> str = '\u001b[1m\u001b[38;5;231mHello World\u001b[0m\u001b[22m'
re = /((?:\u001b\[.*?m)+)([^])([^]|)(?=.*?((?:\u001b\[.*?m)+)|)/
var foo, bar = []

while (null != (foo = str.match(re)))
if ('' !== foo[3]) {
if ('\u001b' === foo[2])
str = ''
else {
bar.push({
'start': foo[1],
'content': foo[2],
'end': foo[4]
})
str = str.replace(re, '$1$3')
}
} else
str = str.replace(re, '$3')

bar
<<< [{start:'\u001b[1m\u001b[38;5;231m',content:'H',end:'\u001b[0m\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m',content:'e',end:'\u001b[0m\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m',content:'l',end:'\u001b[0m\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m',content:'l',end:'\u001b[0m\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m',content:'o',end:'\u001b[0m\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m',content:'',end:'\u001b[0m\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m',content:'W',end:'\u001b[0m\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m',content:'o',end:'\u001b[0m\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m',content:'r',end:'\u001b[0m\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m',content:'l',end:'\u001b[0m\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m',content:'d',end:'\u001b[22m'}]

要处理像 '\u001b[1m\u001b[38;5;231mHello\u001b[0m\u001b[22m World' 这样的字符串:

>>> str = '\u001b[1m\u001b[38;5;231mHello\u001b[0m\u001b[22m World'
re = /((?:\u001b\[.*?m)+)([^])([^]|)(?=.*?((?:\u001b\[.*?m)+)|)/
var foo, bar = []

while (null != (foo = str.match(re)))
if ('\u001b' === foo[2])
str = str.replace(re, '$2$3')
else {
bar.push({
'start': foo[1],
'content': foo[2],
'end': foo[4]
})
str = str.replace(re, '$1$3')
}

bar
<<< [{start:'\u001b[1m\u001b[38;5;231m',content:'H',end:'\u001b[0m\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m',content:'e',end:'\u001b[0m\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m',content:'l',end:'\u001b[0m\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m',content:'l',end:'\u001b[0m\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m',content:'o',end:'\u001b[22m'},{start:'\u001b[1m\u001b[38;5;231m\u001b[0m\u001b[22m',content:'',end:undefined},{start:'\u001b[1m\u001b[38;5;231m\u001b[0m\u001b[22m',content:'W',end:undefined},{start:'\u001b[1m\u001b[38;5;231m\u001b[0m\u001b[22m',content:'o',end:undefined},{start:'\u001b[1m\u001b[38;5;231m\u001b[0m\u001b[22m',content:'r',end:undefined},{start:'\u001b[1m\u001b[38;5;231m\u001b[0m\u001b[22m',content:'l',end:undefined},{start:'\u001b[1m\u001b[38;5;231m\u001b[0m\u001b[22m',content:'d',end:undefined}]

关于javascript - 从输入字符串中获取包含字符及其 ANSI 颜色的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27087013/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com