How to remove random excess of slashes from url?

两盒软妹~` 提交于 2020-06-28 04:19:13

问题


How to remove random excess of slashes from url or just validate it?

For example,

valid statements:

http://domain.com/url/url2

https://domain.com/url/url2

www.domain.com/url/url2

invalid statements:

http://domain.com//url/url2

https://domain.com/////url/url2

www.domain.com/url/////////url2

Thanks for help!


回答1:


Use regular expressions:

require 'uri'
url = URI.parse('https://domain.com/////url/url2')
url.path.gsub! %r{/+}, '/'
p url.to_s



回答2:


this pattern do the job (with http(s) or not) :

"https://domain.com/////url/url2".gsub! %r{(?<!:)/+(?=/)}, ''



回答3:


The other answers do not remove a trailing slash from the URL - which can be important for SEO purposes. There are many ways to do this, but for example:

require 'uri'
url = URI.parse('https://example.com/////url/url2/')
url.path.gsub! %r{/+}, '/'
url.path.sub! %r{/$}, ''

Or:

require 'uri'
url = URI.parse('https://example.com/////url/url2/')
url.path.squeeze!('/')
url.path.chomp!('/')

See: String#squeeze! and String#chomp!.



来源:https://stackoverflow.com/questions/16237086/how-to-remove-random-excess-of-slashes-from-url

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!