I looking for regular expression to use in my javascript code, which give me last part of url without parameters if they exists - here is example - with and without parameters:
https://scontent-fra3-1.xx.fbcdn.net/v/t1.0-9/14238253_132683573850463_7287992614234853254_n.jpg?oh=fdbf6800f33876a86ed17835cfce8e3b&oe=599548AC
https://scontent-fra3-1.xx.fbcdn.net/v/t1.0-9/14238253_132683573850463_7287992614234853254_n.jpg
In both cases as result I want to get:
14238253_132683573850463_7287992614234853254_n.jpg
Here is this regexp
.*\/([^?]+)
and JS code:
let lastUrlPart = /.*\/([^?]+)/.exec(url)[1];
let lastUrlPart = url => /.*\/([^?]+)/.exec(url)[1];
// TEST
let t1 = "https://scontent-fra3-1.xx.fbcdn.net/v/t1.0-9/14238253_132683573850463_7287992614234853254_n.jpg?oh=fdbf6800f33876a86ed17835cfce8e3b&oe=599548AC"
let t2 = "https://scontent-fra3-1.xx.fbcdn.net/v/t1.0-9/14238253_132683573850463_7287992614234853254_n.jpg"
console.log(lastUrlPart(t1));
console.log(lastUrlPart(t2));
May be there are better alternatives?
You could always try doing it without regex. Split the URL by "/" and then parse out the last part of the URL.
var urlPart = url.split("/");
var img = urlPart[urlPart.length-1].split("?")[0];
That should get everything after the last "/" and before the first "?".
Related
I would like this url:
http://bulk-click.startappsdasdasice.com/tracking/adClick?d=scsdc%20cdcsc%20c
to become
bulk-click.startappsdasdasice.com/tracking
I need this kind of pattern for all urls. so the string with the question mark and onward need to be deleted
There are several ways for this one of them is
var url="http://bulk-click.startappsdasdasice.com/tracking/adClick?d=scsdc%20cdcsc%20c";
var suburl=url.substring(0,url.lastIndexOf("/")).replace(/(^\w+:|^)\/\//, '');
console.log(suburl);
You may want to consider using a regular expression
var myRe = new RegExp('([http|https]://)(.+)(\/.+\?)', 'g');
var myArray = myRe.exec('http://bulk-click.startappsdasdasice.com/tracking/adClick?d=scsdc%20cdcsc%20c');
console.log(myArray[2]);
You can use the URL interface to achieve this and substring till the last pathname (/adClick)
let url = new URL('http://bulk-click.startappsdasdasice.com/tracking/adClick?d=scsdc%20cdcsc%20c');
console.log(url.host + url.pathname.substring(0, url.pathname.lastIndexOf('/')))
URL
lastIndexOf()
substring()
Good idea. But use lenght const, because it assigns meaning specifically to this URL, and not to all the meanings of the URL above
const myRe = new RegExp('([http|https]://)(.+)(/.+\?)', 'g');
const myArray = myRe.exec('http://bulk-click.startappsdasdasice.com/tracking/adClick?d=scsdc%20cdcsc%20c');
console.log(myArray[2]);
So I got this function where I retrieve a URL from SP2010. Which gives me a relative URL that does me no good.
e.g.
8;#reports/125/ReportList/closedTasks.rdl
I split this multiple times to retrieve the part I need.
For one var I need the reports/125/ReportList/closedTasks.rdl
So the part after the #. And only split method required to do so.
But for an other var I only need the file name without the extension.
So only closedTasks.
Because of this I need the do multiple split method like below.
Is there a more elegant way to do so?
var relName = ($(this).attr("ows_FileRef")).split("#")[1];
var relNameSub = relName.split("/")[3];
var name = relNameSub.split(".")[0];
You can use .slice() and .lastIndexOf()
var url = "8;#reports/125/ReportList/closedTasks.rdl";
var hash = url.slice(3); // slice the first 3 characters
var fileName = url.slice(url.lastIndexOf("/") + 1, url.lastIndexOf(".")); // slice following last "/" ending at `"."`
console.log(hash, fileName);
You can use the following regex, in order to match two capturing groups as you've defined.
Within context this would look like:
var match = (/^.*#(.*\/([.\w]+)$)/g).exec("8;#reports/125/ReportList/closedTasks.rdl");
// match[1] = "reports/125/ReportList/closedTasks.rdl"
// match[2] = "closedTasks.rdl"
Explanation:
The first capturing group contains everything after a #
The second capturing group (within the first one) contains all the alpha-numeric\dot characters after the last /.
I have regex /[-a-zA-Z0-9#:%._\+~#=]{2,256}\.[a-z]{2,6}\b([-a-zA-Z0-9#:%_\+.~#?&\\=]*)/g for checking valid URL. But it accepts:
https://www.google.com
google.com
https://google.com
dev.google.com
Thats fine with me but I want that when user inputs these values filtered value will be google.com which I have to pass through AJAX but the inputs will be same on UI part ( means browser ). I know this is possible but I can't.
Any reference will be helpfull
Thanks in advance
Try
var regExp = /\w+\.\w+(?=$|\/)/;
var matches1 = regExp.exec("https://www.google.com/images");
var matches2 = regExp.exec("google.com");
var matches3 = regExp.exec("https://google.com");
var matches4 = regExp.exec("dev.google.com");
//matches1[0] contains the value between the parentheses
console.log(matches1[0]);
console.log(matches2[0]);
console.log(matches3[0]);
console.log(matches4[0]);
All of the above will give you 'google.com'
Try the following regular exp. :
var a = /(\w*\.\w*)$|(\w*\.\w*)(\/)/;
Explanation :
first part of OR checks for last occurance of x.y
second part of OR checks for x.y right before starting of "/" keyword
Check either matching section should appear at the end of URL or before any path, fragment or query string:
\w+\.\w+(?=$|[\/?#])
Live demo
I wonder if it's possible to get this part of a string.
Here is my string:
var string = "www.somesite.com/o/images%2Fc834vePyJ3SFVk2iO4rU0ke1cSa2%2F12391381_10205760647243398_2385261683139818614_n.jpg?alt=media&token=7a692a38-6982-474f-bea5-459c987ae575";
Now I want to be able to grab just this part of the string, the file name:
12391381_10205760647243398_2385261683139818614_n.jpg
I tried:
var result = /[^/]*$/.exec(""+url+"")[0];
, but it will return
user%2Fc834vePyJ3SFVk2iO4rU0ke1cSa2%2F12391381_10205760647243398_2385261683139818614_n.jpg?alt=media&token=4c92c4d7-8979-4478-a63d-ea190bec87cf
My Regex is wrong.
Another this is, the file extension can be .png or jpg so it's not fixed to jpg.
You could use a regex to isolate the part you want :
This works :
var string = "www.somesite.com/o/images%2Fc834vePyJ3SFVk2iO4rU0ke1cSa2%2F12391381_10205760647243398_2385261683139818614_n.jpg?alt=media&token=7a692a38-6982-474f-bea5-459c987ae575";
console.log((string.match(/[A-Za-z0-9_]+.(jpg|png|bmp)/))[0].substring(2));
Note that may have to be adapted depending on how much the URL string changes:
var string = "www.somesite.com/o/images%2Fc834vePyJ3SFVk2iO4rU0ke1cSa2%2F12391381_10205760647243398_2385261683139818614_n.jpg?alt=media&token=7a692a38-6982-474f-bea5-459c987ae575";
var out = string.split('?')[0].split('%2F')[2];
console.log(out); // "12391381_10205760647243398_2385261683139818614_n.jpg"
Assuming, you always have an url, first I would decode the encoded / (%2F) characters via:
var string = "www.somesite.com/o/images%2Fc834vePyJ3SFVk2iO4rU0ke1cSa2%2F12391381_10205760647243398_2385261683139818614_n.jpg?alt=media&token=7a692a38-6982-474f-bea5-459c987ae575";
var decodedUrl = decodeURIComponent(string);
and then use a regex:
decodedUrl.match(/[^/]*(?=[?])/)
Mind, that this regex assumes parameters (the part starting with ?...) are present, so if that's not the case, you might have to alter it to your needs.
If the filename always has a .jpg extension:
var url = decodeURIComponent(string);
var filename = url.substring(url.lastIndexOf("/")+1, url.lastIndexOf(".jpg"))
If not:
url = url.substring(url.lastIndexOf("/")+1)
filename = url.substring(0,url.indexOf("?"))
Looking at the string, it appears that the file name is between the second occurrence of "%2F" and the first occurrence of "?" in the string.
The first step is to get rid of the part of the string before the second "%2F". This can be done by splitting the string at every "%2F" and taking the third element in the resulting array.
var intermediate = string.split("%2F")[2]
Then, we need to get rid of everything after the "?":
var file_name = intermediate.split("?")[0]
This should give you the file name from the URL
i want to break a following url
http://www.example.com?name=john&token=3425kkhh34l4345jjjhjhj&uid=09900434&cn=bella&cjid=3344324
into this by eliminating last two parametes i.e. &cn=bella&cjid=3344324
http://www.example.com?name=john&token=3425kkhh34l4345jjjhjhj&uid=09900434
the length of the url may change but the last two parameters remains in that position only. so how can i remove that in a efficient way.
A RegExp is the easiest way for this case:
str = str.replace(/&[^&]*&[^&]*$/,'');
You can use replace with regular expression. If the url is in var url then you can use this one
var new_url = url.replace(/&cn=.*/, '');
you can test it with
var url = 'http:\www.example.com?name=john&token=3425kkhh34l4345jjjhjhj&uid=09900434&cn=bella&cjid=3344324';
console.info(url.replace(/&cn=.*/, ''));
var string = "http://dom.com/?one=1&two=2&three=3&four=4";
string.match(/(.*)&(.*)&(.*)/)[1]; // strips last two parameters
You can use regular expressions to replace the last 2 parameters with the empty string:
var url = "http://www.example.com/?p1=1&p2=2&p3=3&p4=4";
var urlWithoutLast2Parameters = url.replace(/&[^&]+&[^&]+$/,"");
You could use the function IndexOf to find the location of the '&cn' and then just use the substring function to create a new string eliminating the '&cn' portion of the URL, so something like...
var intIndexOf = str.IndexOf('&cn=')
strURL = strURL.substring(0,intCharAt)