Question

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!

Was it helpful?

Solution

Use regular expressions:

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

OTHER TIPS

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

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

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!.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top