Reputation: 902
I am want add string in between the url
I have an url
hostname.com/test1/test
I want to add test2 before the test
it should be hostname.com/test1/test2/test
Do I need break the strings with / and then again build the string by adding tests?
Or is there any other way I can work on?
Upvotes: 0
Views: 122
Reputation: 2100
var url ="hostname.com/test1/test";
var lastSlash = url.lastIndexOf("/");
var output = url.substring(0,lastSlash) + "/test2" + url.substring(lastSlash, url.length);
console.log(output);
Hope this helps.
Upvotes: 2
Reputation: 803
var url = 'hostname.com/test1/test';
var urlParts = url.split('/');
urlParts.splice(urlParts.length-1, 0, "test2");
alert(urlParts.join('/'));
Upvotes: 1