Created
August 24, 2010 23:07
-
-
Save insane-dreamer/548520 to your computer and use it in GitHub Desktop.
Convert links in plain text to html
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# will convert links in a string to html | |
# but will skip over any that are already html links | |
# | |
# i.e. "a string with a http://link.com here." => | |
# "a string with <a href="http://link.com">http://link.com</a> here." | |
# | |
# but "a string with a <a href="http://link.com">existing html link</a> is skipped" | |
# | |
regex = Regexp.new('((?<!=\")(https?:\/\/|www\.)([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?)') | |
gsub(regex, '<a href="\1">\1</a>' ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
PS. Only works with ruby 1.9 (which supports lookbehind)