Skip to content

Instantly share code, notes, and snippets.

@bhalash
Created November 18, 2015 17:05
Show Gist options
  • Select an option

  • Save bhalash/ab3501fb0c22b74423f6 to your computer and use it in GitHub Desktop.

Select an option

Save bhalash/ab3501fb0c22b74423f6 to your computer and use it in GitHub Desktop.
String regex test
str = 'This hyperlink should be stripped out: http://www.bhalash.com. word www.google.ie, more word https://www.twitter.com!'
regex = /(\b(http(s?)|www).*?(?=(\s|[[:punct:]]\s|.(?=$))))/
p = str.scan(regex).each { |url|
link = url[0]
link_text = url[0]
link_href = url[0]
if !link_href.match(/^http(s?):\/\//)
link_href = 'https://' + link_href
end
link_text = link_text.gsub(/^.*?\./, '')
link_html = "<a class='tweet__external-link' href='#{link_href}'>#{link_text}</a>"
p link
p link_href
p link_text
p link_html
p '======'
# str.gsub!(/#{link}/, link_href)
# str.gsub!(/#{link}/, link_text)
str.gsub!(/#{link}/, link_html)
}
p str
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment