Rectangle 27 0

javascript jQuery get last part of URL?


var url = "www.example.com/folder/code/12345";
  var checkExt = /\d$/i.test(url);
  if (checkExt) {
      alert("Yup its a numeric");
  } else {
      alert("Nope");
  }
Note
Rectangle 27 0

javascript jQuery get last part of URL?


var url = "www.example.com/folder/code/12345";
  var checkExt = /\d$/i.test(url);
  if (checkExt) {
      alert("Yup its a numeric");
  } else {
      alert("Nope");
  }
Note
Rectangle 27 0

javascript jQuery get last part of URL?


You realise the lastIndexOf('/') occurs as the last character of the URL/string?

Note
Rectangle 27 0

javascript jQuery get last part of URL?


You realise the lastIndexOf('/') occurs as the last character of the URL/string?

Note
Rectangle 27 0

javascript jQuery get last part of URL?


var URI = 'www.example.com/folder/code/12345/',
    parts = URI.split('/'),
    lastPart = parts.pop() == '' ? parts[parts.length - 1] : parts.pop();
Note
Rectangle 27 0

javascript jQuery get last part of URL?


$(function () {
     siteCode = getLastPartOfUrl('www.example.com/folder/code/12345/');
});

var getLastPartOfUrl =function($url) {
    var url = $url;
    var urlsplit = url.split("/");
    var lastpart = urlsplit[urlsplit.length-1];
    if(lastpart==='')
    {
        lastpart = urlsplit[urlsplit.length-2];
    }
    return lastpart;
}
www.example.com/folder/sub-folder/sub-sub-folder/code/12345

1.Consider what if there is a url like www.example.com/folder/code/12345 (Without '/' forward slash) Than none of the above code will work as per expectation.

There is one best way to take last part of URL is like following which generally has been used in real implementation.

Note
Rectangle 27 0

javascript jQuery get last part of URL?


var URI = 'www.example.com/folder/code/12345/',
    parts = URI.split('/'),
    lastPart = parts.pop() == '' ? parts[parts.length - 1] : parts.pop();

Using .pop() confuses things as it's destructive and changes the parts[] array. If your URI does not have a trailing slash, this fails to return '12345' and instead returns 'code'. Using this version is more straight-forward: lastPart = parts[parts.length-1] !== '' ? parts[parts.length-1] : parts[parts.length-2];

Note
Rectangle 27 0

javascript jQuery get last part of URL?


var str="url";

str.split("/")[3]

Thanks a lot, this got it working

Note
Rectangle 27 0

javascript jQuery get last part of URL?


$(function () {
     siteCode = getLastPartOfUrl('www.example.com/folder/code/12345/');
});

var getLastPartOfUrl =function($url) {
    var url = $url;
    var urlsplit = url.split("/");
    var lastpart = urlsplit[urlsplit.length-1];
    if(lastpart==='')
    {
        lastpart = urlsplit[urlsplit.length-2];
    }
    return lastpart;
}
www.example.com/folder/sub-folder/sub-sub-folder/code/12345

1.Consider what if there is a url like www.example.com/folder/code/12345 (Without '/' forward slash) Than none of the above code will work as per expectation.

There is one best way to take last part of URL is like following which generally has been used in real implementation.

Note
Rectangle 27 0

javascript jQuery get last part of URL?


var str="url";

str.split("/")[3]

Thanks a lot, this got it working

Note