Guys, I have a text where urls are found with double slashes.

What does a regular routine look like for removing one of two rally?

<p><a href="http://site.com//XYZ">abcd</a></p> заменить на <p><a href="http://site.com/XYZ">abcd</a></p> <p><a href="//XYZ">abcd</a></p> заменить на <p><a href="/XYZ">abcd</a></p> 

    2 answers 2

     <?php $str = 'http://site.com//aaa'; echo preg_replace('/(?<!:)[\/]{2,}/','/',$str); // http://site.com/aaa 

    Js

     var str = 'http://site.com//aaa'; console.log(str.replace(/(^|[^:])[/]{2,}/, '$1/', str)); 

    • but this is not javascript? ) - Nofate
    • @Nofate Sorry, Lured - Deonis
    • Added option for JS - Deonis
    • The answer is with an error. - Visman

     Array.prototype.slice.call(document.getElementsByTagName('a')).forEach(function(link) { var href = link.getAttribute('href').replace(/(^|[^:])\/\//, '$1/') console.log(href); link.setAttribute('href', href) }); 
     <p><a href="http://site.com//XYZ">abcd</a></p> заменить на <p><a href="http://site.com/XYZ">abcd</a></p> <p><a href="//XYZ">abcd</a></p> заменить на <p><a href="/XYZ">abcd</a></p>