Botkit for Slack 在对话中使用正则表达式模式

Botkit for Slack using regex patterns in conversations

我 运行 遇到了一个在 botkit 对话中使用正则表达式模式的问题,我无法完全解决,虽然我确信其他人会很快回答,但我完全被难住了.

我正在构建一个将用户信息存储在 JSON 文件中的对话,但在存储条目之前我需要对其进行少量验证。具体来说,输入必须是全名(大于一个的任意数量的单词,中间有 space)或格式为 domain\name 且没有 space 的域用户名,并且正确的域名。

使用 RegExr,我想出了以下 regEx 表达式,它们在该用户界面中匹配,但在放置在 botkit 对话节点的 "pattern" 属性中时将不匹配:

但是当我在 botkit 对话中使用它们时,它们不匹配——所以我不太清楚我做错了什么。

这是使用这些的代码片段:

bot.startConversation(message, function (err, convo) {
    convo.say("I don't know who you are in TFS. Can you tell me?");
    convo.addQuestion("You can say \"no\" or tell me your TFS name or your domain name (eg: \"domain\username)", [
        {
            pattern: bot.utterances.no,
            callback: function (response, convo) {
                convo.say("Okay, maybe another time then.");
                convo.next();
            }
        },
        {
                pattern: '\w+( +\w+)+',
                callback: function (response, convo) {
                convo.say("You said your TFS name is " + response.text);
                convo.next();
            }
        },
        {
            pattern: 'domain+(\+\w+)+',
            callback: function (response, convo) {
                convo.say("You said your TFS name is " + response.text);
                convo.next();
            }
        },
        {
            default: true,
            callback: function (response, convo) {
                convo.say("I didn't understand that.");
                convo.repeat();
                convo.next();
            } 
        }   
    ], {}, 'default');

您需要使用双反斜杠,并修复第一个正则表达式字符串文字中结束单引号之前的反斜杠:

pattern: '\w+( +\w+)+',
pattern: 'domain(\\\w+)+',

第一个模式:

  • \w+ - 1+ 个单词字符
  • ( +\w+)+ - 1 个或多个由 1 个或多个空格组成的序列,然后是 1 个或多个单词字符

域正则表达式:

  • domain - 一个 domain
  • (\\\w+)+ - 出现 1 次或多次
    • \\ - 1 个反斜杠
    • \w+ - 1 个或多个单词字符。