简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM