I would like to remove the "sg" subdirectory from the URL (https://testing.com/sg/features/), resulting in (https://testing.com/features/).
Let's say if my window.location.href is https://testing.com/sg/features/, I need to edit and remove "sg" subdirectory from it, and then put it into a new location without hardcoding it. Meaning that it will dynamically get the URL and then go to the location without "sg" (https://testing.com/features/).
var url = 'https://testing.com/sg/features/';
var x = url.split('/');
console.log(x[3]); //result: sg
I was only able to get the sg out from the URL but am not sure how do I remove it.
I would say that the best way is to split by '/', look for a section which exactly is the string you want to remove and reassemble the new string while ignoring the matches. This code removes every /sg/ in the string
let thisLocation = "https://testing.com/sg/features/";
let splitLoc = thisLocation.split('/');
let newLocation = "";
for (let i = 0; i < splitLoc.length; i++){
if (splitLoc[i] !== "sg")
newLocation += splitLoc[i] + '/';
}
newLocation = newLocation.substring(0, newLocation.length - 1);
You could also do a global .replace while looking for '/sg/'. Your choice