问题
I have looked around but I haven't really found an answer that seals the deal 100% for my individual situation.
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!
EDIT
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.
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...
Thanks.
回答1:
autolink will do it. It's been extracted into it's own gem...
https://github.com/tenderlove/rails_autolink
Example usage:
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>"
回答2:
Try:
^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
from the IETF Specification on URLS. It matches ALL URLS including "ftp://..." so you may need to tweak it somewhat.
来源:https://stackoverflow.com/questions/13198997/regex-to-match-urls-in-ruby-on-rails