例如,用户输入 @John_Smith hi, what's up!?
我想给John_Smith的对象打电话,通知他让他注意。
我该如何存档?
username = @comment ??????(I have no idea how to code it here)
@user = User.find_by_username(username)
current_user.send_message(@user, body, subject)
判断方式为@username(一个空格)
最佳答案
username = @comment.scan(/(?<=^@)\w+(?=[\s|~])/).first
控制台测试:
2.0.0-p195 :001 > s = "@John_Smith~hi"
=> "@John_Smith~hi"
2.0.0-p195 :002 > s.scan(/(?<=^@)\w+(?=[\s|_])/).first
=> "John_Smith"
编辑:现在接受 ~
或空格作为分隔符。
关于ruby-on-rails - 如何使用正则表达式获取用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17129584/