Created
November 18, 2015 17:05
-
-
Save bhalash/ab3501fb0c22b74423f6 to your computer and use it in GitHub Desktop.
String regex test
This file contains hidden or 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
| 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