I want to create a Hubot script which listens for strings that look like:
thanks Hubot
where Hubot
is the bots name. If we assume that bot is always named hubot
, then it's trivial:
robot.hear /thanks hubot/i, (msg) ->
...
But the name of course can vary, though is contained in the robot.name
variable.
Question: how do you interpolate the bot's name into the regular expression? This question seems to indicate you can just use block regular expressions, but I tried:
robot.hear ///thanks #{robot.name}///i, (msg) ->
....
And it didn't seem to work.
Edit: I have a workaround, I can extract the second word and compare to the bot's name, like so:
robot.hear /thanks (.*)/i, (msg) ->
name = msg.match[1]
if name.toLowerCase() is robot.name.toLowerCase()
....
But this feels rather kludgy, I'd like to be able to just interpolate a variable into the regex itself.
The
///
s in Coffeescript don't just allow interpolation, they engage the full extended mode which means that whitespace is no longer significant inside those regexes, ie. if you want to match a space then you have to use something like\s
So try this: