Substring between two strings? - javascript

var str = "https://cdn.fbsbx.com/v/t59.2708-21/68856895_411975049700005_8580443955521388544_n.xls/test.xls?_nc_cat=106&_nc_oc=AQmcm2PVCUFFyUJDJgLs3ZYM4Dg12PX1Wv48Fm0LJ8-Qi8duxOpEVrD2uFgrD9e1pDOXcLpJmbtjbveAm12xczd2&_nc_ht=cdn.fbsbx.com&oh=18eab18ae1d1cf2a95084bba0a002163&oe=5D8F8124";
var n = str.substring(str.indexOf("\\.") +1 , str.indexOf("?_nc_cat="));
I have this string but my output is :
https://cdn.fbsbx.com/v/t59.2708-21/68856895_411975049700005_8580443955521388544_n.xls/test.xls
How can i get only this .xls?
My other string is :
https://scontent.xx.fbcdn.net/v/t1.15752-9/70629455_2730574856953299_3640328874664919040_n.png?_nc_cat=100&_nc_oc=AQm0m5jryh7zAzyj2R-w7ke0DKQgHM7aYaVkkRjPYDUQ6g-FUAWqVwhnr7qxqISkWMdiNhtp7e8gYMA6gss58poN&_nc_ad=z-m&_nc_cid=0&_nc_zor=9&_nc_ht=scontent.xx&oh=1cbb98fb9484bd3f26b6058808cca889&oe=5E36459B
but again im not getting just word "png" im getting from start link.

I'd use a regular expression, and match word characters while looking ahead for ?:
const getFileType = str => str.match(/\w+(?=\?)/)[0];
console.log(getFileType("https://cdn.fbsbx.com/v/t59.2708-21/68856895_411975049700005_8580443955521388544_n.xls/test.xls?_nc_cat=106&_nc_oc=AQmcm2PVCUFFyUJDJgLs3ZYM4Dg12PX1Wv48Fm0LJ8-Qi8duxOpEVrD2uFgrD9e1pDOXcLpJmbtjbveAm12xczd2&_nc_ht=cdn.fbsbx.com&oh=18eab18ae1d1cf2a95084bba0a002163&oe=5D8F8124"));
console.log(getFileType('https://scontent.xx.fbcdn.net/v/t1.15752-9/70629455_2730574856953299_3640328874664919040_n.png?_nc_cat=100&_nc_oc=AQm0m5jryh7zAzyj2R-w7ke0DKQgHM7aYaVkkRjPYDUQ6g-FUAWqVwhnr7qxqISkWMdiNhtp7e8gYMA6gss58poN&_nc_ad=z-m&_nc_cid=0&_nc_zor=9&_nc_ht=scontent.xx&oh=1cbb98fb9484bd3f26b6058808cca889&oe=5E36459B'));

Simply, Just you have to use the LastIndexof() in the right way to achieve this
str = "https://scontent.xx.fbcdn.net/v/t1.15752-9/70629455_2730574856953299_3640328874664919040_n.png?_nc_cat=100&_nc_oc=AQm0m5jryh7zAzyj2R-w7ke0DKQgHM7aYaVkkRjPYDUQ6g-FUAWqVwhnr7qxqISkWMdiNhtp7e8gYMA6gss58poN&_nc_ad=z-m&_nc_cid=0&_nc_zor=9&_nc_ht=scontent.xx&oh=1cbb98fb9484bd3f26b6058808cca889&oe=5E36459B";
url = str.substring(0,str.indexOf("?"));
ext = str.substring(url.lastIndexOf(".")+1, url.length);
The same will work for any other string as well.

Since it seems you're trying to get the last value from pathname which is preceded by . from URL, so you can use URL API
Simply parse the URL with URL api, take the patname value and split on . and take the last element from splitted array.
var str = "https://cdn.fbsbx.com/v/t59.2708-21/68856895_411975049700005_8580443955521388544_n.xls/test.xls?_nc_cat=106&_nc_oc=AQmcm2PVCUFFyUJDJgLs3ZYM4Dg12PX1Wv48Fm0LJ8-Qi8duxOpEVrD2uFgrD9e1pDOXcLpJmbtjbveAm12xczd2&_nc_ht=cdn.fbsbx.com&oh=18eab18ae1d1cf2a95084bba0a002163&oe=5D8F8124";
let valueExtractor = (str) =>{
let urlParse = new URL(str)
return urlParse.pathname.split('.').pop()
}
console.log(valueExtractor(str))
console.log(valueExtractor("https://cdn.fbsbx.com/v/t59.2708-21/68856895_411975049700005_8580443955521388544_n.xls/test.png?_nc_cat=106&_nc_oc=AQmcm2PVCUFFyUJDJgLs3ZYM4Dg12PX1Wv48Fm0LJ8-Qi8duxOpEVrD2uFgrD9e1pDOXcLpJmbtjbveAm12xczd2&_nc_ht=cdn.fbsbx.com&oh=18eab18ae1d1cf2a95084bba0a002163&oe=5D8F8124"))

Related

get first string between slashes after url

I have the following link
http://bulk-ccc.aaaa.com/tracking/food?
I need to get the first string after the url which in this case is
tracking
and also the second string
food
how this can be done with JS?
You can use URL interface to parse a URL string. You can try this:
const str = "http://bulk-ccc.aaaa.com/tracking/food?";
const url = new URL(str);
let parts = url.pathname ? url.pathname.split('/').filter(v => !!v) : [];
console.log(parts);
From this parts array you can get your require parts.
You can split the string to get it into an array and then get the needed amount of elements
window.location.pathname.split('/').filter((x, i) => x && i < 3)

How do I get the 4 digit user ID of a discord user?

I’m wondering how I can get the 4 digit user ID of a discord user from a message. For example, PruinaTempestatis#8487. Can someone help me?
If the number always appears after a single hash # then you can simply do a split on that string and get the second value of the array like this:
USING SPLIT
var str = 'PruinaTempestatis#8487';
var digits = str.split('#')[1];
console.log(digits);
USING REGEX
var str = 'PruinaTempestatis#8487';
var digits = str.replace( /^\D+/g, '');
console.log(digits);
USING SUBSTRING
var str = 'PruinaTempestatis#8487';
var digits = str.substr(str.indexOf('#')+1, str.length);
console.log(digits);
Using user.tag
message.channel.send(message.user.tag)
Split their tag into an array and get the last element.
member.user.tag.split('#')[-1];

How to check if part of string is present in array or not

I have got of array symbols as shown below
var sourcesymbols = ["ERT", "UBL" , "AMAZING"];
I am getting the following news title from rss feed
you experts are amazing
How to check if the content present in the rssfeedstring is present under the sourcesymbols array or not ??
For example rssfeedstring has word amazing and it is also present under sourcesymbols
please let me know how to achive this .
I have tried to convert the rssfeedstring to uppercase then i am not sure how to use the indexOf on the string .
rssfeedstring = rssfeedstring.toUpperCase();
please let em know if there is any better approach also for doing this as the array will have 2000 symbols
http://jsfiddle.net/955pfz01/3/
You can use regex.
Steps:
Convert the array to string with join using |(OR in regex) as glue
Use \b-word boundary to match exact words
Use i flag on regex to match irrespective of the case. So, don't have to change the case of string.
Escape the slashes as using RegExp constructor requires string to be passed and \ in string is used as escape following character.
test can be used on regex to check if the string passes the regex.
var sourcesymbols = ["ERT", "UBL", "AMAZING"];
var mystr = 'you experts are amazing';
var regex = new RegExp("\\b(" + sourcesymbols.join('|') + ")\\b", 'i'); // /\b(ERT|UBL|AMAZING)\b/i
alert(regex.test(mystr));
You can also use some
Convert the string to array by using split with \s+. This will split the string by any(spaces, tabs, etc) one or more space character
Use some on splitted array
Convert the string to uppercase for comparing
Check if the element is present in array using indexOf
var mystr = 'you experts are amazing';
var sourcesymbols = ["ERT", "UBL", "AMAZING"];
var present = mystr.toUpperCase().split(/\s+/).some(function(e) {
return sourcesymbols.indexOf(e) > -1;
});
alert(present);
Try using Array.prototype.map() , Array.prototype.indexOf() to return matched text, index of matched text within sourcesymbols
var sourcesymbols = ["ERT", "UBL" , "AMAZING"];
var mystr = 'you experts are amazing';
var res = mystr.split(" ").map(function(val, index) {
var str = val.toUpperCase(), i = sourcesymbols.indexOf(str);
return i !== -1 ? [val, i] : null
}).filter(Boolean);
console.log(res)
jsfiddle http://jsfiddle.net/955pfz01/6/

Regex to get string in URL before optional character

I have a URL which may be formatted like this: http://domain.com/space/all/all/FarmAnimals
or like this: http://domain.com/space/all/all/FarmAnimals?param=2
What regular expression can I use to return the expression FarmAnimals in both instances?
I am trying this:
var myRegexp = /\.com\/space\/[a-zA-Z0-9]*\/[a-zA-Z0-9]*\/(.*)/;
var match = myRegexp.exec(topURL);
var full = match[1];
but this only works in the first instance, can someone please provide an example of how to set up this regex with an optional question mark closure?
Thank you very much!
/[^/?]+(?=\?|$)/
Any non-/ followed by either ? or and end-of-line.
I wouldn't write my own regex here and let the Path class handle it (if those are your two string formats).
string url = "http://domain.com/space/all/all/FarmAnimals";
//ensure the last character is not a '/' otherwise `GetFileName` will be empty
if (url.Last() == '/') url = url.Remove(url.Length - 1);
//get the filename (anything from FarmAnimals onwards)
string parsed = Path.GetFileName(url);
//if there's a '?' then only get the string up to the '?' character
if (parsed.IndexOf('?') != -1)
parsed = parsed.Split('?')[0];
You could use something like this:
var splitBySlash = topURL.split('/')
var splitByQ = splitBySlash[splitBySlash.length - 1].split('?')
alert(splitByQ[0])
Explanation:
splitBySlash will be ['http:','','domain.com', ... ,'all','FarmAnimals?param=2'].
Then splitByQ will grab the last item in that array and split it by ?, which becomes ['FarmAnimas','param=2'].
Then just grab the first element in that.
This
.*\/(.*?)(\?.*)?$
Should capture the part of the string you are looking for as the group 1 (and the query after ? in group 2, if needed).
var url = 'http://domain.com/space/all/all/FarmAnimals?param=2';
//var url = 'http://domain.com/space/all/all/FarmAnimals';
var index_a = url.lastIndexOf('/');
var index_b = url.lastIndexOf('?');
console.log(url.substring(index_a + 1, (index_b != -1 ? index_b : url.length)));

find special text from string in javascript

I have string like #ls/?folder_path=home/videos/
how i can find last text from string? this place is videos
other strings like
#ls/?folder_path=home/videos/
#ls/?folder_path=home/videos/test/testt/
#ls/?folder_path=seff/test/home/videos/
We could use a few more example strings, but based off of your one and only example, here's a rough regex to get you started:
.*?/\?.*?/(.*?)\//
EDIT:
Based on your extended examples:
.*?/\?.*/(.*?)\//
This regex will consume text until the second to last / and capture until the last / in the string.
This will work even if the string doesn't end in /
var str;
var re = /\w+(?=\/?$)/;
str = "#ls/?folder_path=home/videos/"
str.match(re) ; //# => videos
str = "#ls/?folder_path=home/videos/test/testt/"
str.match(re) ; //# => testt
str = "#ls/?folder_path=seff/test/home/videos/"
str.match(re) ; //# => videos
str = "#ls/?folder_path=home/videos/test/testt"
str.match(re) ; //# => testt
\/([^\/]*)\/?$
This regex will match all non / between the last two /. Where the last / is optional. The $ is matching the end of the string.
Your resulting string is then in the first capturing group (because of the ()) $1
You can test it here
There are many ways to do this. One of them:
var str = '#ls/?folder_path=home/videos/'.replace(/\/$/,'');
alert(str.substr(str.lastIndexOf('/')+1)); //=> videos
Alternative without using replace
var str = '#ls/?folder_path=home/videos/'
,str = str.substr(0,str.length-1)
,str = str.substr(str.lastIndexOf('/')+1);
alert(str); //=> videos
If your data is consistent like this string, this is a simple split based way to retreive
your required string: http://jsfiddle.net/EEkLP/
var str="#ls/?folder_path=home/videos/";
var strArr = str.split("/");
alert(strArr[strArr.length-2]);
If it always ends with / then this will works.
var str = '#ls/?folder_path=home/videos/';
var arr = str.split('/');
var index = arr.length-2;
console.log(arr[index]);
If the last word always enclosed with forward slashes, then you can try this -
".+\/([^\/]+)\/$"
or in regex notation
/.+\/([^\/]+)\/$/

Categories