正则表达式在Ruby on Rails中匹配URL [英] Regex to Match Urls in Ruby on Rails
问题描述
我环顾四周但是我还没有真正找到一个能够100%解决这个问题的答案。
I have looked around but I haven't really found an answer that seals the deal 100% for my individual situation.
在我的Ruby应用程序中,我有一个用户共享系统,成员可以发布'东西'。好吧,有时会员发布链接,我注意到随着网站的增长,越来越多的用户抱怨没有可点击的链接。
In my Ruby app, I have a user share system where members can post 'stuff'. Well, sometimes members post links, and I have noticed as the site grows, more and more users are complaining about there not being clickable links.
我如何利用正则表达式匹配帖子中链接的网址。此外,我如何在我的模型中应用它?
How would I utilize regular expressions to match urls for links in posts. Furthermore, how would I apply this in my Model?
感谢您的帮助,在我的所有问题中,这对我来说真的很重要哈哈!
Thanks for you help, out of all my questions, this is a real important one for me ha!
编辑
我认为我的初始帖子太模糊了......
I suppose my init post was too vague...
我更想弄清楚如何在我的模型中使用正则表达式而不是使用正则表达式。
I am more trying to figure out HOW to IMPLEMENT the use of regular expressions into my model instead of which regular expression to use.
模型是 class Share
。会员的帖子呈现为<%= share.content%>
,内容是我的共享表中的列以及可访问的属性...
The model is class Share
. And the member's post is rendered as <%= share.content %>
with content being a column in my Share table as well as an accessible attribute...
谢谢。
推荐答案
autolink会做到这一点。它被提取到它自己的宝石中......
autolink will do it. It's been extracted into it's own gem...
https: //github.com/tenderlove/rails_autolink
示例用法:
auto_link("Go to http://www.rubyonrails.org and say hello to david@loudthinking.com")
# => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
# say hello to <a href=\"mailto:david@loudthinking.com\">david@loudthinking.com</a>"
这篇关于正则表达式在Ruby on Rails中匹配URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!