Get last part of url with / at the end - javascript

I'm trying to get the last part of this url:
http://webserver/Foo/login/
the code that I wrote:
var location = window.location.href.split('/').pop();
will return an empty string 'cause after the / there is nothing.
I need to get at least the previous part, so in this case, login.
How can I do this?

The solution using String.replace()(used to replace possible / at the end of the string) and String.split() functions:
var url = 'http://webserver/Foo/login/',
last_section = url.replace(/\/+$/, '').split('/').pop();
console.log(last_section);

const getLastPartURL = url => {
const parts = url.split('/');
const length = parts.length;
return parts[length - 1] == '' ? parts[length - 2] : parts[length - 1]
}
console.log(getLastPartURL('http://webserver/Foo/login/'))
console.log(getLastPartURL('http://webserver/Foo/login'))
console.log(getLastPartURL('http://webserver/Foo/'))
console.log(getLastPartURL('http://webserver/Foo'))

This should do the trick:
location.pathname.match(/[^/]*(?=\/*$)/)[0]
Explanation:
Location.pathname is a string containing an initial '/' followed by the path of the URL.
String.prototype.match(regexp) returns an array of the matches.
[^/]* matches anything but a slash, and that zero or more times.
(?=\/*$) matches a slash zero or more times at the end of the string, while not including it.
There is always exactly one match, so we retrieve it with [0].
Example:
For all these URLs the output is login:
http://webserver/Foo/login
http://webserver/Foo/login/
http://webserver/Foo/login//

Related

Extract the portions of the string using REGULAR Expression in node.js

I tried to extract the Only URL part not the Path, for example
if the url is https://example.com/news/cricket
the output should be https://example.com/
but i am not strong on regular expression.
data = "https://example.com/news/cricket";
var name = data.substring(0, data.lastIndexOf("/"));
console.log(name);
this is what i have tried
but output is:-
https://example.com/news
Expected output is
https://example.com/
Thank for healping guys
lastIndexOf() returns the index of the last /, which is the one before cricked, not before news.
indexOf() takes an optional starting index, you can first search for // and then search for the next / after that.
You need to add 2 to the index of // to skip over it. And you have to add 1 to the index of / so you include the / in the result.
data = "https://example.com/news/cricket";
var name = data.substring(0, data.indexOf("/", data.indexOf("//")+2)+1);
console.log(name);
With a regexp, use a regexp that matches up to the first // and then to the next /.
var data = "https://example.com/news/cricket";
var match = data.match(/^.*?\/\/.*?\//);
if (match) {
var name = match[0];
console.log(name);
}

Javascript : Get value of first and second slash

How do i get first and second slash of a URL in javascript?
URL : http://localhost:8089/submodule/module/home.html
Now i want the value /submodule/module
Below is the code i have been trying
window.location.pathname.substring(0, window.location.pathname.indexOf("/",2))
This got me only /submodule
window.location.pathname.substring(0, window.location.pathname.lastIndexOf("/",window.location.pathname.lastIndexOf("/")-1))
Also this didnt work. Can anyone please guide where i am going wrong.
This should work, it take everything exept the last element of pathname:
let result = window.location.pathname.split('/').slice(0,-1).join('/') + '/'
Only the 1st and 2de item:
let result = window.location.pathname.split('/').slice(0,2).join('/') + '/'
Handle path without file :
// ex: /foo/bar/path.html > foo/bar/
// ex: /foo/bar/ > foo/bar/
let result = (window.location.pathname[window.location.pathname.length -1] !== '/') ? window.location.pathname.split('/').slice(0,-1).join('/') + '/' : window.location.pathname
You can use the split() function, for example like this:
var url = 'http://localhost:8089/submodule/module/home.html';
var parts = url.split('/');
console.log('/' + parts[3] + '/' + parts[4]);
The output will be:
/submodule/module
Try this:
var pathParts = window.location.pathname.split('/');
var result = `${pathParts[1]}/${pathParths[2]}`;
Using a regular expression:
var url = 'http://localhost:8089/submodule/module/home.html'; //or window.location.pathname
var re = /\/\/.+(\/.+\/.+)\/.+/
re.exec(url)[1];
This expression basically says the url is in the format
//[anything](/submodule/module)/[anything]
and to get everything in parentheses.
Functional style
/\/[^/]*\/[^/]*/.exec(window.location.pathname)[0]
You could use a regular expression:
var url = ...
var part = url.match(/https?:\/\/.*?(\/.*?\/.*?)\/.*/)[1]
Explanation:
http Match the group 'http'
s? Match 1 or 0 's'
: Match a semicolon
\/\/ Match '//'
.*? Match anything (non-greedy)
( Start capturing block (Everything captured will be an array element)
\/*?\/.*? Something that looks like /.../...
) End capturing block
\/.* Something that looks like /...
The output of the match method will be an array with 2 elements. The first one is the whole matched string, and the second is the captured group.

Regex for removing part of URL param

I am trying use regex to remove a particular param in a url.
//Here are the scenarios of what I want to remove in the url
'?pIds=123,2311' => ''
'?pIds=123,2311&deal=true' => '?deals=true'
'?pIds=123' => ''
'?pIds=123&deals=true' => '?deals=true'
'&pIds=123,2311' => ''
'&pIds=123,2311&deals=true' => '&deals=true'
'&pIds=123' => ''
'&pIds=123&deals=true' => '&deals=true'
const a = '?pIds=123,2311&deals=true';
a.replace(/&?pIds=\d+,?\d+/i, '');
Is this possible to create a single regex for these scenarios? How can I conditionally have ? or & there if pIds is the first or middle param, respectively?
You can use this regex in Javascript for searching:
/[?&]pIds=[^&]*$|([?&])pIds=[^&]*&/
RegEx Breakup:
[?&]pIds=[^&]*$: Match ? or & followed by pIds=. $ ensures this is the only parameter in query string.
|: OR
([?&])pIds=[^&]*&: Match ? or & followed by pIds= followed by &. This is the case where there is one more parameter in query string.
Code:
var arr=['?pIds=123,2311',
'?pIds=123,2311&deal=true',
'?pIds=123',
'?pIds=123&deals=true',
'&pIds=123,2311',
'&pIds=123,2311&deals=true',
'&pIds=123',
'&pIds=123&deals=true'];
var re = /[?&]pIds=[^&]*$|([?&])pIds=[^&]*&/;
for (i=0; i<arr.length; i++) {
console.log(arr[i], ' => ', arr[i].replace(re, '$1'));
}
RegEx Demo
The regex to identify the block you are talking about is something like the following:
((?<=\?)|\&)pIds=\d+(,\d+)?
The first part is a "positive lookbehind" for a question mark, which will match if there is a question mark before pIds, but it will not include the question mark as part of the match. An ampersand also works, but it is included as part of the match, so it will get deleted.
I also made the treatment of the optional comma and numbers a little bit clearer. You always have one block of numbers (\d+), optionally followed by a comma and another block of numbers.
Edit: In my original post, I forgot to treat the ampersands properly. If the string begins with a question mark and there is no ampersand, you want to delete the question mark. If it starts with a question mark and ends with an ampersand, you want to delete the ampersand at the end. If it both begins and ends with an ampersand, you need to delete one of them. If it begins with an ampersand and does not end with one, you need to delete the one at the beginning. The result is slightly more complicated and looks like this:
\?pIds=\d+(,\d+)?($|[^&])|(?<=\?)pIds=\d+(,\d+)?\&|\&pIds=\d+(,\d+)
The first case takes care of no ampersand at the end (($|[^&]) corresponds to either end-of-line or no ampersand). The second case takes care of beginning with ? and ending with &. The third case takes care of the remaining two scenarios, where there is a & at the beginning.
There are loads of ways to do this. Here is a version without regex:
let url1 = 'foo.bar?pIds=123,2311&deals=true&foo=bar';
let parsedUrl;
let queryParts;
// Get the query string from the URL
parsedUrl = url1.split('?');
// Split the query string so we get each key value then filter so we dont get the pIds
queryParts = parsedUrl[1].split('&').filter(q => q.indexOf('pIds') === -1);
// set URL to the original hostname and a ? if we have a query still
url1 = (queryParts.length > 0 ? '?' : '')
// Join the query parts
url1 += queryParts.join('&')
console.log(url1);
More examples:
let url2 = 'foo.bar?pIds=123,2311';
parsedUrl = url2.split('?');
queryParts = parsedUrl[1].split('&').filter(q => q.indexOf('pIds') === -1);
url2 = parsedUrl[0] + (queryParts.length > 0 ? '?' : '')
url2 += queryParts.join('&')
console.log(url2);
let url3 = 'foo.bar?foo=bar&pIds=123,2311';
parsedUrl = url3.split('?');
queryParts = parsedUrl[1].split('&').filter(q => q.indexOf('pIds') === -1);
url3 = parsedUrl[0] + (queryParts.length > 0 ? '?' : '')
url3 += queryParts.join('&')
console.log(url3);

How do I extract data from this URL using javascript?

I need to build a string from the data contained in this url using javascript/jQuery:
http://www.example.com/members/admin/projects/?projectid=41
The string returned should look as follows:
/ajax/projects.php?projectid=41
Obviously if there is no query string present, the method should still return a string of the same format minus the query string. e.g.
http://www.example.com/members/admin/messages/
should return...
/ajax/messages.php
I've made numerous attempts, all met without success due to my poor grasp of regular expressions, and it feels as though the ore I rad on the subject the more I am confusing myself.
If someone could help it would be greatly appreciated.
EDIT: The 'admin' portion of the url is a users 'username' and could be anything.
Here's a function that will take your URL and return a new one according to the rules you've listed above:
function processURL(url) {
var base = "", query = "";
var matches = url.match(/([^\/\?]+)(\/$|$|\?|\/\?)/);
if (matches) {
base = matches[1];
matches = url.match(/\?[^\?]+$/);
if (matches) {
query = matches[0];
}
}
return("/ajax/" + base + ".php" + query);
}
And, a test app that shows it working on a bunch of URLs: http://jsfiddle.net/jfriend00/UbDfn/
Input URLs:
var urls = [
"http://www.example.com/members/admin/projects/?projectid=41",
"http://www.example.com/members/bob/messages/",
"http://www.example.com/members/jill/projects/",
"http://www.example.com/members/alice/projects?testid=99",
"http://www.example.com/members/admin/projects/?testid=99"
];
Output results:
/ajax/projects.php?projectid=41
/ajax/messages.php
/ajax/projects.php
/ajax/projects.php?testid=99
/ajax/projects.php?testid=99
To explain, the first regular expression looks for:
a slash
followed by one or more characters that is not a slash and not a question mark
followed by one of the four sequences
/$ a slash at the end of the string
$ end of the string
? a question mark
/? a slash followed by a question mark
The point of this regex is to get the last segment of the path that comes before either the end of the string or the query parameters and it's tolerant of whether the last trailing slash is there or not and whether there are any query parameters.
I know exactly what you are trying to do. In order to do it your way just split your string on question mark and then use last item form your array.
var data = your_url.split('?');
var newUrl = '/ajax/projects.php' + (data.length > 1 ? data[length-1] : "");
and you will have your url.
But what you can do is execute same url using your Script just add one parameter IsAjax=true and then check it in codebehind and execute your ajax logic.
e.g.
$('#somelink').onclick(function(){
$.ajax({ url: $(this).href, data { IsAjax: true } .... }
});
Using this way you will have more robust app.
I'll assume that by
http://www.example.com/members/admin/messages/
should return...
/ajax/members.php
you meant - should return...
/ajax/messages.php
If that is the case try
var query = url.split('?');
var paths = query[0].split('/');
var path = paths.pop();
if (path == '') //when there is trailing slash
path = paths.pop();
if (query.length == 1) //no query string
newurl = '/ajax/' + path + '.php';
else //query string
newurl = '/ajax/' + path + '.php?' + query[1];
I'm sure it can be made simpler and better, but that might give you a start.
var str = "http://www.example.com/members/admin/projects/?projectid=41";
var newStr = "/ajax/" + str.split("/").slice(-2).join(".php");
console.log(newStr);

Javascript substring() trickery

I have a URL that looks like http://mysite.com/#id/Blah-blah-blah, it's used for Ajax-ey bits. I want to use substring() or substr() to get the id part. ID could be any combination of any length of letters and numbers.
So far I have got:
var hash = window.location.hash;
alert(hash.substring(1)); // remove #
Which removes the front hash, but I'm not a JS coder and I'm struggling a bit. How can I remove all of it except the id part? I don't want anything after and including the final slash either (/Blah-blah-blah).
Thanks!
Jack
Now, this is a case where regular expressions will make sense. Using substring here won't work because of the variable lengths of the strings.
This code will assume that the id part wont contain any slashes.
var hash = "#asdfasdfid/Blah-blah-blah";
hash.match(/#(.+?)\//)[1]; // asdfasdfid
The . will match any character and
together with the + one or more characters
the ? makes the match non-greedy so that it will stop at the first occurence of a / in the string
If the id part can contain additional slashes and the final slash is the separator this regex will do your bidding
var hash = "#asdf/a/sdfid/Blah-blah-blah";
hash.match(/#(.+?)\/[^\/]*$/)[1]; // asdf/a/sdfid
Just for fun here are versions not using regular expressions.
No slashes in id-part:
var hash = "#asdfasdfid/Blah-blah-blah",
idpart = hash.substr(1, hash.indexOf("/"));
With slashes in id-part (last slash is separator):
var hash = "#asdf/a/sdfid/Blah-blah-blah",
lastSlash = hash.split("").reverse().indexOf("/") - 1, // Finding the last slash
idPart = hash.substring(1, lastSlash);
var hash = window.location.hash;
var matches = hash.match(/#(.+?)\//);
if (matches.length > 1) {
alert(matches[1]);
}
perhaps a regex
window.location.hash.match(/[^#\/]+/)
Use IndexOf to determine the position of the / after id and then use string.substr(start,length) to get the id value.
var hash = window.location.hash;
var posSlash = hash.indexOf("/", 1);
var id = hash.substr(1, posSlash -1)
You need ton include some validation code to check for absence of /
This one is not a good aproach, but you wish to use if you want...
var relUrl = "http://mysite.com/#id/Blah-blah-blah";
var urlParts = [];
urlParts = relUrl.split("/"); // array is 0 indexed, so
var idpart = = urlParts[3] // your id will be in 4th element
id = idpart.substring(1) //we are skipping # and read the rest
The most foolproof way to do it is probably the following:
function getId() {
var m = document.location.href.match(/\/#([^\/&]+)/);
return m && m[1];
}
This code does not assume anything about what comes after the id (if at all). The id it will catch is anything except for forward slashes and ampersands.
If you want it to catch only letters and numbers you can change it to the following:
function getId() {
var m = document.location.href.match(/\/#([a-z0-9]+)/i);
return m && m[1];
}

Categories

Resources