[英]Ruby - Editing URLs
I used the following question, Remove "www", "http://" from string , to remove the HTTP
and HTTPS
from my url. 我使用以下问题, 从string中删除“ www”,“ http://” ,以从我的URL中删除
HTTP
和HTTPS
。 Now I want to remove the path on the end of the url as well and can't seem to get the pattern. 现在,我也想删除url末尾的路径,而且似乎无法获取该模式。 This is the code I am trying to use:
这是我尝试使用的代码:
str = str.sub(/^https?\:\/\//, '').sub(/^www./,'').sub(/^\/*/,'')
The first two .sub
work as expected and remove the HTTP
, HTTPS
and www.
前两个
.sub
正常工作,并删除HTTP
, HTTPS
和www.
from the url but the path ie http://URL/path/to/remove/
stays with the URL. 从url开始,但路径即
http://URL/path/to/remove/
与URL保持一致。 As can be seen above I have tried the following pattern /^\\/*/
in the third sub but it doesn't remove the path on the end of the url. 从上面可以看出,我在第三子菜单中尝试了以下模式
/^\\/*/
,但是它并未删除网址末尾的路径。 What pattern will remove all characters after the base URL? 什么模式会删除基本URL之后的所有字符?
You could: 你可以:
require 'uri'
URI('http://stackoverflow.com/questions/24252071/ruby-editing-urls').host
# => "stackoverflow.com"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.