Search code examples
regexcoffeescripthubotslack

Hubot Not Responding to regex


I am working on a hubot slack integration, but have hit a bit of a brick wall. I am trying to get hubot to respond to this regex

(\d{4}\-){3}\d{4}

But for some reason it will not work.

Code Snippet

robot.respond /(\d{4}\-){3}\d{4}/i, (msg) ->
    msg.send "Words, Words, Words"

Any help would be greatly appreciated.

Regards, Austin


Solution

  • In Hubot, the respond regex is anchored, so the whole string should match.

    Thus, you need to either add .* or[\s\S]* on both ends of the regex. Also, I recommend to add word boundaries \b to make sure you match a whole word.

    Thus, if there are newline symbols in the input, use

    /[\s\S]*\b(\d{4}\-){3}\d{4}\b[\s\S]*/
    

    If there are no newline symbols, just use

    /.*\b(\d{4}\-){3}\d{4}\b.*/
    

    Note the the case insensitive modifier is redundant here as there are no letters in the pattern.