Ruby linkify for urls in strings
I might be missing some context, but why re-invent the wheel? Have you tried auto_link
in actionpack
?
$ gem install actionpack
$ irb -f --prompt simple
>> require 'action_view'
>> include ActionView::Helpers
>> auto_link("abc http://google.com xyz")
=> "abc <a href=\"http://google.com\">http://google.com</a> xyz"
>> auto_link("abc <a href='http://google.com'>google</a> xyz")
=> "abc <a href='http://google.com'>google</a> xyz"
C# code to linkify urls in a string
It's a pretty simple task you can acheive it with Regex and a ready-to-go regular expression from:
- http://regexlib.com/
var html = Regex.Replace(html, @"^(http|https|ftp)\://[a-zA-Z0-9\-\.]+" +
"\.[a-zA-Z]{2,3}(:[a-zA-Z0-9]*)?/?" +
"([a-zA-Z0-9\-\._\?\,\'/\\\+&%\$#\=~])*$",
"<a href=\"$1\">$1</a>");
You may also be interested not only in creating links but in shortening URLs. Here is a good article on this subject:- Resolve and shorten URLs in C#
- Regular Expression Workbench at MSDN
- Converting a URL into a Link in C# Using Regular Expressions
- Regex to find URL within text and make them as link
- Regex.Replace Method at MSDN
- The Problem With URLs by Jeff Atwood
- Parsing URLs with Regular Expressions and the Regex Object
- Format URLs in string to HTML Links in C#
- Automatically hyperlink URL and Email in ASP.NET Pages with C#
Ruby on Rails - Converting Twitter @mentions, #hashtags and URLs within a string
The twitter-text gem has pretty much all the work covered for you. Install it manually (gem install twitter-text
, use sudo if needed) or add it to your Gemfile (gem 'twitter-text'
) if you are using bundler and do bundle install
.
Then include the Twitter auto-link library (require 'twitter-text'
and include Twitter::Autolink
) at the top of your class and call the method auto_link(inputString)
with the input string as the parameter and it will give you the auto linked version
Full code:
require 'twitter-text'
include Twitter::Autolink
myString = "I like using @twitter, because I learn so many new things! [line break]
Read my blog: http://www.myblog.com #procrastination"
linkedString = auto_link(myString)
If you output the contents of linkedString, you get the following output:I like using @<a class="tweet-url username" href="https://twitter.com/twitter" rel="nofollow">twitter</a>, because I learn so many new things! [line break]
Read my blog: <a href="http://www.myblog.com" rel="nofollow">http://www.myblog.com</a> <a class="tweet-url hashtag" href="https://twitter.com/#!/search?q=%23procrastination" rel="nofollow" title="#procrastination">#procrastination</a>
Rails 3 Make Text into a Clickable Link
So say your object is in @post
In the display, do <%= link_to @post.url, @post.url %>
That should "linkify" your URL so that users can click it.
Regex to make links clickable (in only 'a href' and not 'img src')
JavaScript lacks support of negative lookbehinds in regular expressions. Here's the simple workaround:
var content = '<a href="http://google.com">Google.com</a> and http://google.com';
var re = /((?:href|src)=")?(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig;
content = content.replace(re, function (match, attr) {
if (typeof attr != 'undefined') {
return match;
}
return '<a target="_blank" href="' + match + '">' + match +'</a>';
});
But you should avoid parsing HTML with RegExp. Here's why. Replacing the 'auto_link' method in Ruby on Rails 3.1
Rinku is a drop-in replacement for Rails 3.1 auto_link
.
Auto-linking functionality has been removed from Rails 3.1, and is instead offered as a standalone gem, rails_autolink. You can choose to use Rinku instead.
require 'rails_rinku'
The rails_rinku package monkeypatches Rails with an auto_link
method that mimics 100% the original one, parameter per parameter. It's just faster.
Related Topics
How to Remove Backslashes from a JSON String
Spawning an Independent Thread or Process in Ruby
Belongs_To Presence in Rails 5 Not Working
Algorithm to Render a Horizontal Binary-Ish Tree in Text/Ascii Form
Alias_Method on Activerecord::Base Results in Nameerror
Why Can't Net::Ftp Connect to Server
Axlsx - Formatting Text Within a Cell
Activerecord Join Table for Legacy Database
Ruby - Append Content at The End of The Existing S3 File Using Fog
Completely Random Identifier of a Given Length
Sorting a Hash in Ruby Based on Value and Then Key
How to Render Blob Images in a Prawn Document
How to Flush HTML to The Wire in Sinatra
What Hash Function Does Ruby Use
Overriding Instance Variable Array's Operators in Ruby
Prawn Doesn't Seem to Push Layout Down When Using Repeat(:All)