1

I'm relatively new to JS and pretty confused. I have a URL string: https://a/b/c/d/e/f. These are all dynamic characters and change often (could be https://x/s/g/e/h/d for example) In some cases I want to remove only f, in which I use LastIndexOf. In other cases I want to remove both the last and second to last EG: e/f. How can this be done successfully? I tried using a split but this just replaced the '/' with ',' for some reason.

working f example

var url = https://a/b/c/d/e/f
var new = url.substring(0, url.lastIndexOf('/') +1);

current split e/f example

var new = url.split('/')
console.log(new[new.length -2]);

This prints as: https:,,a,b,c,d,e,f,

Uciebila
  • 481
  • 2
  • 9
  • 27

5 Answers5

2

Here you are

const str = 'https://a/b/c/d/e/f'

function removeSegments(url, times) {
    const segments = url.split('/')
    return segments.slice(0, segments.length - times).join('/')
}

console.log(removeSegments(str, 1)) // 'https://a/b/c/d/e'
console.log(removeSegments(str, 2)) // 'https://a/b/c/d'
Nurbol Alpysbayev
  • 19,522
  • 3
  • 54
  • 89
1

Here is one option, using regex replacement. To remove the final path only:

url = "https://a/b/c/d/e/f";
url = url.replace(/\/[^/]+$/mg, "");

To remove the final two paths:

url = "https://a/b/c/d/e/f";
url = url.replace(/\/[^/]+\/[^/]+$/mg, "");
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • Thanks for this, when I add the reg ex in, on replace(/**\** it gives an error reading: unterminated Regular expression literal. – Uciebila Nov 16 '18 at 11:45
  • @Uciebila Your comment makes no sense, and I can only comment that my answer solved your problem in fewer lines of code than the answer you accepted. – Tim Biegeleisen Nov 16 '18 at 13:36
  • From some research I found thatbecause my code is within a react app, it is a .js and not a .jsx. Which meant that using the above code threw an error that wouldnt be fixed until I changed my file extension, which would break the react app. With the way I added the accepted answer into my code it only needed two lines – Uciebila Nov 16 '18 at 13:57
  • Strange. It is too bad that your framework is limiting what you can and cannot do. – Tim Biegeleisen Nov 16 '18 at 13:58
0

You can easily do that using the URL API.

You need to split the pathname by / and then use Array#slice to take only the parts you want. Then join those again by /.

Here is an example:

var url = new URL('https://a/b/c/d/e/f');

console.log(
  url.origin +
  url.pathname.split('/').slice(0, -2).join('/')
);
31piy
  • 23,323
  • 6
  • 47
  • 67
0

split() creates an array of substrings that is passed in the split function. So when you write

url.split('/') it will create an array of substrings with / as delimeter.

The answer could be :

get the index of the element till when you want to remove your string by using :

var index = indexOf(x)

then pass it in the function.

var str1 = str.substr(0, index)

str1 will be your answer

vertika
  • 1,284
  • 1
  • 8
  • 22
0

You can use:

var num;
for ( var i=0; i< num; i++) {
    url= url.substring(0, url.lastIndexOf('/'));
}
SamTebbs33
  • 5,507
  • 3
  • 22
  • 44
simona
  • 15
  • 4