Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eliminate only double and additional slashes and replace with one slash

Tags:

javascript

I am facing a problem. I want to replace double or additional slashes from the URL with just one slash. But I am doing the wrong way. The requirement is the slashes should be in the middle or end of the url, should not match with the protocol double slashes e.g. http:// and https://. But my regex is also matching these slashes. How can I ignore the protocol slashes?

const example1 = `https://example.com/home2`;
const example2 = `http://example.com/home2/`;
const example3 = `http://example.com//home2///`;
const example4 = `http://example.com/////home2///`;
const example5 = `http://example.com/////home2///heloo//////`;
const example6 = `http://example.com////home2/////////////`;
const example7 = `https://example.com////home2/////////////`;
const example8 = `http://localhost:9000/////`;

const patt = /\/\/+/g;

const res1 = patt.test(example1) ? example1.replace(patt, '/') : example1;
const res2 = patt.test(example2) ? example2.replace(patt, '/') : example2;
const res3 = patt.test(example3) ? example3.replace(patt, '/') : example3;
const res4 = patt.test(example4) ? example4.replace(patt, '/') : example4;
const res5 = patt.test(example5) ? example5.replace(patt, '/') : example5;
const res6 = patt.test(example6) ? example6.replace(patt, '/') : example6;
const res7 = patt.test(example7) ? example7.replace(patt, '/') : example7;
const res8 = patt.test(example8) ? example8.replace(patt, '/') : example8;

// The results should be something like this.
console.log(res1); // https://example.com/home2 => https://example.com/home2
console.log(res2); // http://example.com/home2/ => http://example.com/home2/
console.log(res3); // http://example.com//home2/// => http://example.com/home2/
console.log(res4); // http://example.com/////home2/// => http://example.com/home2/
console.log(res5); // http://example.com/////home2///heloo////// => http://example.com/home2/heloo/
console.log(res6); // http://example.com////home2///////////// => http://example.com/home2/
console.log(res7); // https://example.com////home2///////////// => https://example.com/home2/
console.log(res8); // http://localhost:9000///// => http://localhost:9000/
like image 631
John Doe Avatar asked Oct 19 '25 03:10

John Doe


2 Answers

A negative lookbehind expression would work: (?<!:)\/\/+

Lookbehind expressions allow you check characters leading the rest of the match without actually including them in the match. Negative lookbehind tests that those leading characters do not match. In this case, (?<!:) is the negative lookbehind, ensuring that there is not a colon before the rest of the expression but doesn't include that leading character in the final match.

like image 93
Ouroborus Avatar answered Oct 21 '25 17:10

Ouroborus


You can pass a function to string replace. You can replace it by omitting the first occurrence.

let str = 'http://example.com//home1///';

str = str.replace(/\/\/+/g, (i => m => !i++ ? m : '/')(0));
console.log(str);
like image 28
shehanpathi Avatar answered Oct 21 '25 17:10

shehanpathi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!