auto.waitFor();
console.show();
try {
    events.observeNotification();
    events.onNotification(function (notification) {
        if (notification.title == '泡泡马特抢') {
            const text = notification.getText();
            // const regex = /(?:\[.*?\])?.*?启动/;
            const regex = new RegExp('(?:[.*?])?.*?启动');
            regex.test(text);
        }
    });
} catch (e) {
    toast('请手动开启通知权限:打开Hamibot→点击左上角→打开通知读取权限');
}
为什么上面的正则无法匹配到,请你帮我修改,并成功匹配到‘启动’
回答
文档地址 https://docs.hamibot.com/
你的正则表达式存在两个问题:
- [.*?]实际表示匹配任意字符(包括方括号本身),应该用- $$.*?$$匹配方括号内的内容
- 需要添加全局匹配标志 g和非贪婪模式
修改后的代码:
auto.waitFor();
console.show();
try {
    events.observeNotification();
    events.onNotification(function (notification) {
        if (notification.title == '泡泡马特抢') {
            const text = notification.getText();
            // 匹配格式:[任意内容]启动(支持中文方括号)
            const regex = new RegExp('$$.*?$$启动', 'g');
            if(regex.test(text)) {
                toast("匹配成功");
            }
        }
    });
} catch (e) {
    toast('请手动开启通知权限:打开Hamibot→点击左上角→打开通知读取权限');
}
示例匹配文本:
[抢购提醒]启动
[PM]启动
                ℹ️ 本回答由 AI 生成,内容仅供参考