remove duplicate forward slashes from the URL

后端 未结 4 1825
南方客
南方客 2021-01-04 03:34

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://localh         


        
相关标签:
4条回答
  • 2021-01-04 03:44

    I know there're different answers solving the same problem, just another approach :)

    var s = "http://www.some-url.com//path//to";
    var res = s.replace(/(https?:\/\/)|(\/)+/g, "$1$2");
    
    0 讨论(0)
  • 2021-01-04 03:50

    You can use:

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

    Working Demo

    Update: Already answered by Halcyon

    0 讨论(0)
  • 2021-01-04 04:06

    This question has been answered before...

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

    DEMO

    0 讨论(0)
  • 2021-01-04 04:07

    To remove extra forward slashes, the below code works fine for me

    $path = preg_replace('#/+#', '/', $path);

    0 讨论(0)
提交回复
热议问题