JavaScript Removing last forwardslash and the text before it remove last forward slash javascript,remove last forward slash c#,

I have an URL looking like this:

https://www.website.com/dk/da/home/category/

I am trying to remove the last forward slash and the text before it, untill it reaches the new forwardslash. Meaning i would like the new URL to look like this:

https://www.website.com/dk/da/home/

I am trying to use substring to achieve this, but i run into problems because I always have a forward slash at the end of the URL.

var to = url.lastIndexOf('/');
to = to == -1 ? url.length : to + 1;
newUrl = url.substring(0, to);
Answer:1

string.lastIndexOf can take a second parameters indicating from which index the search must be done (starting from the end)

so you can do this to get the last which is not the last character of the string :

let url = "https://www.website.com/dk/da/home/category/"

var to = url.lastIndexOf('/', url.length - 2);
to = to == -1 ? url.length : to + 1;
newUrl = url.substring(0, to);

console.log(newUrl)
Answer:2

other way:

str='https://www.website.com/dk/da/home/category/';

arr = str.split('/');

arr.pop();
arr.pop();

str = arr.join('/');

str = str + '/';
Answer:3

I have a function to call API from the server like this: getDataSet(callback) { request.open('POST', `${apiPath}`); request.setRequestHeader('Content-Type', 'application/x-...

I have a function to call API from the server like this: getDataSet(callback) { request.open('POST', `${apiPath}`); request.setRequestHeader('Content-Type', 'application/x-...

  1. json data response example
  2. json data response
  3. json data response ajax
  4. json data response jquery
  5. json response data format
  6. json response data type
  7. json response data count
  8. json response data undefined
  9. json response data python
  10. json response data model
  11. get json data response
  12. sample json data response
  13. send json data response
  14. json binary data response
  15. jbuilder json_data_response
  16. c# json data response
  17. access json response data
  18. axios json response data
  19. json.parse(response.data)
  20. laravel json response data

I have a JSON structure like this: [ {"menuId":"1001","depth":"1","parentId":"0"}, {"menuId":"1002","depth":"1","parentId":"0"}, {"menuId":"1003","depth":"2","parentId":"1001"}, {"...

I have a JSON structure like this: [ {"menuId":"1001","depth":"1","parentId":"0"}, {"menuId":"1002","depth":"1","parentId":"0"}, {"menuId":"1003","depth":"2","parentId":"1001"}, {"...

I'm trying to write a mocha test which passes on a stream error but fails if the stream ends without an error. Detecting the error is no problem, but the finish handler is always called, even if the ...

I'm trying to write a mocha test which passes on a stream error but fails if the stream ends without an error. Detecting the error is no problem, but the finish handler is always called, even if the ...

I'm building a photo gallery in react js. Obviously it's going to have to be responsive and I've tackled this by setting values like so in the component's render method: let thumbWidth = window....

I'm building a photo gallery in react js. Obviously it's going to have to be responsive and I've tackled this by setting values like so in the component's render method: let thumbWidth = window....

  1. change value react
  2. change value react-select
  3. change props value react
  4. change context value react
  5. change input value react
  6. change state value react
  7. change props value react native
  8. change textinput value react native
  9. change ref value react
  10. change state value react native
  11. change animated value react native
  12. change text value react native
  13. change checkbox value react
  14. change value onclick react
  15. change const value react
  16. change array value react
  17. change textarea value react
  18. change context value react hooks
  19. change const value react native
  20. change input value react testing library