我正在为Minecraft服务器制作一个包装器,而且我一直坚持为Ages的这一个Regex匹配问题。基本上我想匹配以下部分
[04:57:40] [Server thread/INFO]: <§c§lAdmin §7§oUser1§r> msg
[04:57:50] [Server thread/INFO]: <§c§lAdmin §7§oUser2§r> msg
[04:58:16] [Server thread/INFO]: <§c§lAdmin §7§oUser1§r> msg
[04:58:28] [Server thread/INFO]: <§4§lCoOwner §7§oUser2§r> msg
[04:58:33] [Server thread/INFO]: <§c§lAdmin §7§oUser1§r> msg
^-------------------^<--That
现在,对于那些不熟悉我的用户名的东西,有三个部分:
[\w\d]{3,16}
轻松满足这一要求。因此,我需要将这3个部分分开。
有没有办法在JS RegExp中执行此操作?我能想到的唯一方法是使用lookbehinds。制作一个JS函数解析它会更好吗?如果是这样,我将如何去做呢?
编辑1:这是我当前的正则表达式(?:[§&\d\w ]){0,16}?(\w{3,16})(?:[§&\d\w ]){0,16}
,给出以下输入行:
<§4&btest &r&rKaidenP§r> woo
模式组#1是rKaidenP
。它需要只是KaidenP
答案 0 :(得分:0)
如果我理解corectly next charfafter§和&amp;应该被忽略:
所以我会这样做:
const getMessageInfo = line => {
line = line.replace(/[\&§]./g, '')
const info = line.match(/<(\S+)\s*(\S+?)?>\s(.*)/)
return {
role: info[2] ? info[1] : 'User',
username: info[2] || info[1],
message: info[3]
}
}
const data = [
'[04:57:40] [Server thread/INFO]: <Admin User1> msg',
'[04:57:50] [Server thread/INFO]: <Admin User2> msg',
'[04:58:16] [Server thread/INFO]: <Admin User1> msg',
'[04:58:28] [Server thread/INFO]: <CoOwner User2> msg',
'[04:58:33] [Server thread/INFO]: <Admin User1> msg',
'[04:58:33] [Server thread/INFO]: <User1> msg',
'<§4&btest &r&rKaidenP§r> woo'
]
for (const line of data) {
const msg = getMessageInfo(line)
console.log(line)
console.log(msg)
}