Over a million developers have joined DZone.

Convert Links In Text To Html

DZone's Guide to

Convert Links In Text To Html

Free Resource
I'm not sure of a great way to match cnn.com with this... but it works for the other urls in the text string.

body_html = "I hate cnn.com and www.cnn.com even http://cnn.com and OMFG especially http://www.cnn.com and https://cnn.com"
regex = Regexp.new '((https?:\/\/|www\.)([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)'
body_html.gsub!( regex, '\1' )

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}