Ruby 正则表达式url转换中包含不必要的字符
我用正则表达式检查url 将正文中包含的url处理为html 不必要的字符进入,所以我不想包含不必要的字符 我的正则表达式如下Ruby 正则表达式url转换中包含不必要的字符,ruby,regex,Ruby,Regex,我用正则表达式检查url 将正文中包含的url处理为html 不必要的字符进入,所以我不想包含不必要的字符 我的正则表达式如下 body => "https://www.yahoo.com/<br /><br />sample<br /><br/>https://www.yahoo.com/" url =>"https://www.yahoo.com/" text => "<!-- BEGIN app/views/topic
body
=> "https://www.yahoo.com/<br /><br />sample<br /><br/>https://www.yahoo.com/"
url
=>"https://www.yahoo.com/"
text
=> "<!-- BEGIN app/views/topics/_link_thumbnail_description.html.slim -->\n\n<a class=\"c-grid__quotation--link\" target=\"_blank\" href=\"https://www.yahoo.com/\"><div class=\"c-grid__quotation text--s-md p-topic__quotation__border c-border-r-5\">\n <div class=\"c-flex\">\n <div class=\"c-grid__quotation--main\">\n <img src=\"https://s.yimg.com/dh/ap/default/130909/y_200_a.png\" alt=\"Y 200 a\" />\n </div>\n <div class=\"c-grid__quotation--side\">\n <div class=\"c-grid__quotation--side-title text--b\">\n Yahoo\n </div>\n <div class=\"c-grid__quotation--side-description\">\n News, email and search are just the beginning. Discover more every day. Find your yodel.\n </div>\n <div class=\"c-grid__quotation--side-url\">\n www.yahoo.com\n </div>\n </div>\n </div>\n</div></a><!-- END app/views/topics/_link_thumbnail_description.html.slim -->"
def convert_url_to_text(body, url, text)
reg_url = Regexp.escape("#{url}")
body.gsub!(/(#{reg_url}$|#{reg_url}[\W\/])/){ |s| "#{text}"}
end
但url将在正文中显示如何使不
URI.extract”https://www.yahoo.com/
样本
https://www.yahoo.com/"
#⇒ ["https://www.yahoo.com/", "https://www.yahoo.com/"]
不要手动进行解析。使用:
URI.extract”https://www.yahoo.com/
样本
https://www.yahoo.com/"
#⇒ ["https://www.yahoo.com/", "https://www.yahoo.com/"]
/(https:\/\/www\.yahoo\.com\/$|https:\/\/www\.yahoo\.com\/[\W\/])/
URI.extract "https://www.yahoo.com/<br />
<br />sample<br /><br/>https://www.yahoo.com/"
#⇒ ["https://www.yahoo.com/", "https://www.yahoo.com/"]