Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

remove duplicate forward slashes from the URL

Tags:

How can I remove duplicate forward slashes from the a url, but keep the // which comes after http: so that the URL does not break.

http://localhost//example/author/admin/// 

should be

http://localhost/example/author/admin/ 

I'm trying this, but it will remove only the last slash. I want to remove all double

abc = 'http://localhost//example/author/admin///'; clean_url = abc.replace(/\/$/,''); alert(clean_url); 

The following checks only three slashes.

clean_url = abc.replace("///", ""); alert(clean_url); 

I want to remove all the duplicate slashes.

like image 511
user3761459 Avatar asked Jun 24 '14 08:06

user3761459


People also ask

What does 2 forward slashes mean in a URL?

A double slash in the URL path is valid and will respond in the browser, but is typically unwelcome, as this could cause duplicate content issues if the CMS delivers the same content on two URLs (i.e. single slash and double slash).

What are the slashes in a URL?

The addition of a slash at the end of a URL instructs the web server to search for a directory. This speeds the web page loading because the server will retrieve the content of the web page without wasting time searching for the file.


2 Answers

You can use:

abc.replace(/([^:]\/)\/+/g, "$1"); 

Working Demo

Update: Already answered by Halcyon

like image 87
Milind Anantwar Avatar answered Sep 26 '22 20:09

Milind Anantwar


This question has been answered before...

var str = 'http://localhost//example/author/admin///';     var clean_url = str.replace(/([^:])(\/\/+)/g, '$1/');  alert(clean_url); 

DEMO

like image 32
martynas Avatar answered Sep 23 '22 20:09

martynas