I have a URL like this:
http://www.url/name/something/#/venue
I need to grab the 'venue' from each URL. I have:
var currentUrl = $(location).attr('href');
var urlParts = currentUrl.split( "/" );
The split breaks up the URL.
I'm not sure how to grab the last part of urlParts.
urlParts[urlParts.length - 1];
Just grab the last element of the urlParts array:
var lastPart = urlParts[urlParts.length - 1];
Instead of $(location) why not just use location object and location.hash? Like this:
location.hash.replace('#/', '')
For url like http://www.url/name/something/#/venue it will give you venue.
You can get it like this:
var url = document.URL.split('#/')[1];
var currentUrl = $(location).attr('href');
var urlParts = currentUrl.split( "/" );
alert(urlParts[urlParts.length-1]);
The fiddle here.
To grab the last element in the array urlParts, just use:
var last = urlParts[urlParts.length-1];
As an alternate way to do this, you could do:
var last = currentUrl.substr( currentUrl.lastIndexOf("/")+1 );
Related
How can I remove some part of an url and add some query before returning it?
Example:
locahost:8080/product/orders/1.
I want to remove the orders/1 and add /?query="sample".
Use replace function:
location.replace("locahost:8080/product/?query='sample'")
You can get the url by simply doing window.location.href. You can then edit it by copying it to some new var newURL = window.location.href;
newUrl = newUrl.replace('/orders/1', '/?query=\"sample\"');
window.location.href = newUrl; // execute this to pass on the parameters to the current page.
Suppose you have url like this in variable1, let say like this
var variable1 = 'http://locahost:8080/product/orders/1'; //here you can get the actual browser url using `window.location.href`and assign it to `variable1`
just use replace function:
var final_text = variable1.replace('/orders/1','?query=sample');
you will get the following output, you do console.log(final_text);
http://locahost:8080/product?query=sample
You can try something like
var url = window.location.href;
var query = "somestring" ;
window.location.replace(url + "&" + somestring);
removing the / pieces:
var newloc = url.substring(0, url.search("/")) + query;
window.location.replace(newloc);
My URL looks like this
stackoverflow.com/questions/ask/format/return
I need to get only format/return from the above URL. I'm able to assign the complete URL to a variable. Currently i'm doing it on split
url.split("/")[4]
url.split("/")[5]
And this is not Generic. What is the better way to achieve this?
The shortest, cleanest way to do this is by using slice on the splitted URL:
url.split("/").slice(-2).join("/")
Just use the length to help you index from the end:
var res = url.split('/')
var last = res[res.length-1]
var pre_last = res[res.length-2]
A genetic solution,
Var URL = url.split("/"); //
Last = URL[URL.length-1]; // return
LastBefore = URL[URL.length-1]; //format
url = "stackoverflow.com/questions/ask/format/return"
URL = url.split("/");
console.log(URL[URL.length-1]) // return
console.log(URL[URL.length-2]) //format
Look for the last and penultimate values:
let spl = url.split("/");
alert(spl[spl.length-2]+' and '+spl[spl.length-1]);
I'd parse the url to a URL, then use match on the pathname. This way it will also work should you have any searchparams (?foo=bar) in your url
const s = "https://stackoverflow.com/questions/ask/format/return";
const uri = new URL(s);
const m = uri.pathname.match(/([^\/]*)\/([^\/]*)$/);
console.log(m);
Note that you'll get an array holding three entries - 0 being the path combined, 1 the first and 2 the last param.
You can use a simple regex /.*\/(.*\/.*)/) to extract exactly what you want.
str.match(/.*\/(.*\/.*)/).pop()
var str = "stackoverflow.com/questions/ask/format/return",
res = str.match(/.*\/(.*\/.*)/).pop();
console.log(res);
var parts = url.split("/");
lastTwo = [parts.pop(), parts.pop()].reverse().join("/");
I have this URL
http://192.168.22.124:3000/temp/box/c939c38adcf1873299837894214a35eb
I want to replace the last part of my URL which is c939c38adcf1873299837894214a35eb with something else.
How can I do it?
Try this:
var url = 'http://192.168.22.124:3000/temp/box/c939c38adcf1873299837894214a35eb';
somethingelse = 'newhash';
var newUrl = url.substr(0, url.lastIndexOf('/') + 1) + somethingelse;
Note, using the built-in substr and lastIndexOf is far quicker and uses less memory than splitting out the component parts to an Array or using a regular expression.
You can follow this steps:
split the URL with /
replace the last item of array
join the result array using /
var url = 'http://192.168.22.124:3000/temp/box/c939c38adcf1873299837894214a35eb';
var res = url.split('/');
res[res.length-1] = 'someValue';
res = res.join('/');
console.log(res);
Using replace we can try:
var url = "http://192.168.22.124:3000/temp/box/c939c38adcf1873299837894214a35eb";
var replacement = 'blah';
url = url.replace(/(http.*\/).*/, "$1" + replacement);
console.log(url);
We capture everything up to and including the final path separator, then replace with that captured fragment and the new replacement.
Complete guide:
// url
var urlAsString = window.location.href;
// split into route parts
var urlAsPathArray = urlAsString.split("/");
// create a new value
var newValue = "routeValue";
// EITHER update the last parameter
urlAsPathArray[urlAsPathArray.length - 1] = newValue;
// OR replace the last parameter
urlAsPathArray.pop();
urlAsPathArray.push(newValue);
// join the array with the slashes
var newUrl = urlAsPathArray.join("/");
// log
console.log(newUrl);
// output
// http://192.168.22.124:3000/temp/box/routeValue
You could use a regular expression like this:
let newUrl = /^.*\//.exec(origUrl)[0] + 'new_ending';
I have a url strings like
"example.com/Apps/Visitor/visitscanner" ,
"example.com/Apps/Seatings/visitscanner
i want to select the directory name after the ""http://example.com/Apps/"
for above example url's, if my url string is "http://example.com/Apps/Visitor/visitscanner" this one, then results should be like "Visitor".
How can i do this in javascript?
you could use split(), like:
var str = "example.com/Apps/Visitor/visitscanner";
var parts = str.split('/');
console.log( parts[parts.length - 2] ); //Visitor
You can do this with regex if you want to match URLs like "http://example.com/Apps/Visitor/visitscanner/foo" or "http://example.com/Apps/Visitor"...
var url = "http://example.com/Apps/Visitor/visitscanner";
var match = /^(https?:\/\/)?example.com\/apps\/([^/]+)/i.exec(url);
var directory = match[2]; // get the second match group ("Visitor")
Live Demo
you can do it with a regex like :-
var url = "http://example.com/Apps/Visitor/visitscanner";
var dirName = /\/\/.+?\/Apps\/(.+?)(?=\/)/g.exec(url)[1];
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?
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.
var url = 'hostname.com/test1/test';
var urlParts = url.split('/');
urlParts.splice(urlParts.length-1, 0, "test2");
alert(urlParts.join('/'));