我希望找到办法让 hubot 到达这里一个变量。 例如
name = "Peter"
module.exports = (robot) ->
robot.hear /hello name/i, (msg) ->
msg.send "Peter?! No I am Hubot."
我已经尝试过如下所示的“#{}”语法,但没有任何结果。
name = "Peter"
module.exports = (robot) ->
robot.hear /hello #{name}/i, (msg) ->
msg.send "Peter?! No I am Hubot."
任何帮助将不胜感激。
问候,
奥斯汀
最佳答案
由于您的正则表达式不是常量,因此您应该使用 new Regex()
:
Using the constructor function provides runtime compilation of the regular expression. Use the constructor function when you know the regular expression pattern will be changing, or you don't know the pattern and are getting it from another source, such as user input.
代码
name = "Peter"
regx = new Regex("hello #{ name }", 'i')
module.exports = (robot) ->
robot.hear regx, (msg) ->
msg.send "Peter?! No I am Hubot."
编辑 以名称作为参数
module.exports = (robot, name) ->
regx = new Regex("hello #{ name }", 'i')
robot.hear regx, (msg) ->
msg.send "#{ name }?! No I am Hubot."
关于coffeescript - Hubot 听觉变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34098300/