Here is the regex:
/(?<=[^:\s])(\/+\/)/g
It finds multiple slashes in storing URLs after the protocol independently of it.
It also handles relative protocol URLs starting with // .
@Test public void shouldReplaceMultipleSlashes() { assertEquals("http://google.com/?q=hi", replaceMultipleSlashes("http://google.com///?q=hi")); assertEquals("https://google.com/?q=hi", replaceMultipleSlashes("https:////google.com//?q=hi")); assertEquals("//somecdn.com/foo/", replaceMultipleSlashes("//somecdn.com/foo///")); } private static String replaceMultipleSlashes(String url) { return url.replaceAll("(?<=[^:\\s])(\\/+\\/)", "/"); }
Literally means:
(\/+\/) - find the group: /+ one or more slashes, and then / slash(?<=[^:\s]) - which follows the group (* posiive lookbehind) of this (* negative set) [^:\s] , which excludes : colon and \s spacesg - global search flag
Andrew
source share