javascriptregexbotkit

How to accept multiple vales from Botkit hears method?


Code:

controller.hears([map (.*) (.*) into (.*)){
  var value = message.match[1];
        var source = message.match[2];
        var dest = message.match[3];   }

when input is map 12 grams to kilograms it works fine but when input is map 12 square meter into square centimeter then output is value = 12 square, source= meter and dest = square centimeter

How to modify the code so that the value field accepts only number so that square meter goes to source field


Solution

  • You may use

    map (\d+) (.*) into (.*)
    

    See the regex demo

    The first capturing group is now (\d+) and will only capture 1 or more digits.

    You may also match 1+ whitespaces in between values using \s+ (so, tabs and other whitespace will also be matched, 1 or more occurrences):

    map\s+(\d+)\s+(.*?)\s+into\s+(.*)
    

    See this regex demo.

    Details