I want to replace all multiple slashes in URL, apart from those in protocol definition ('http[s]://', 'ftp://' and etc). How do I do this?
This code replaces without any exceptions:
url.gsub(/\/\/+/, '/')
You just need to exclude any match which is preceeded by :
url.gsub(/([^:])\/\//, '\1/')
I tried using URI:
require "uri"
url = "http://host.com//foo//bar"
components = URI.split(url)
components[-4].gsub!(/\/+/, "/")
fixed_url = [components[0], "://", components[2], components[-4]].join
But that seemed hardly better than using a regex.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With