DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Convert Links In Text To Html

  • submit to reddit
        I'm not sure of a great way to match with this... but it works for the other urls in the text string.

body_html = "I hate and even and OMFG especially and"
regex = '((https?:\/\/|www\.)([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)'
body_html.gsub!( regex, '<a href="\1">\1</a>' )