Reputation: 222
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.
Upvotes: 0
Views: 1055
Reputation: 353
Try:
^(([^:/?#]+):)?(//([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?
from the IETF Specification on URLS. It matches ALL URLS including "ftp://..." so you may need to tweak it somewhat.
Upvotes: 2
Reputation: 19899
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 [email protected]")
# => "Go to <a href=\"http://www.rubyonrails.org\">http://www.rubyonrails.org</a> and
# say hello to <a href=\"mailto:[email protected]\">[email protected]</a>"
Upvotes: 1