How to extract a string from url using javascript - javascript

I have following string:
How can I extract text (i.e.ABC) from the string?

You can try anchorObject.pathname or anchorObject.href.match(/\/([^/]+)$/)[1] if you just want the last path.
You still need to get the anchorObject from the DOM.
const parser = new DOMParser();
const elm = parser.parseFromString('', 'text/html');
const anchorObject = elm.getElementsByTagName('a')[0];
And voilĂ .

If you are not sure of what the length of the variable you're looking for is, you can use this syntax.
let string = ' ';
console.log(string.split('"')[1].split('/')[3]);
If you know what you're looking for ( for Example: ABC ) you can find index of ABC, and splice the string.
let variable = 'ABC'
console.log(string.substr(string.search("ABC"), variable.length))
Breakdown the string until you get the variable.

Related

How to find the exact value of href in <a> tag using regex

I have a string that contains an <a> tag with href attributes. I need to find the regex which matches only value of hrefs.
VALUE HERE <-- string to find
TWITTOR VALUE HERE <-- another string to find
I would like to get exact http://value.com or www.twittor.com. I searched the site for an answer, many solutions were found, but they all match additional information, not the value itself.
Like this one: Regex to find Href value matches href="http://value.com" and so the others.
Use a regular expression with a capturing group (enclosed in ()). Then use .exec and grab the last item from the return value of .exec:
const inputA = 'VALUE HERE';
const inputB = 'TWITTOR VALUE HERE';
const last = list => list[list.length - 1];
const extract = input => /href="(.*)"/g.exec(input);
console.log(last(extract(inputA)));
console.log(last(extract(inputB)));
Using the native DOM parser might be a viable alternative to a regex. Pass in the string, parseFromString, and then return the href attribute of the first child element in the body of the document returned by the parser.
const str1 = 'VALUE HERE';
const str2 = 'TWITTOR VALUE HERE';
const parser = new DOMParser();
function getHref(parser, str) {
return parser
.parseFromString(str, 'text/html')
.body.firstChild.getAttribute('href');
}
console.log(getHref(parser, str1));
console.log(getHref(parser, str2));

Get specific portion of a URL string

Here is a working code I have to get a specific portion of a URL string:
const movieName = "frozen_2019";
const courseName = "olaf_is_enjoying_living_his_dream_1";
const source = "your majesty from https://example.com/english/courses/frozen_2019/olaf_is_enjoying_living_his_dream_1/voices/references/translate/do%20you%20hear%20that.mp3";
console.log(getSourceError(source)); // result
function getSourceError(source) {
const a = source.substring(source.indexOf(courseName) + courseName.length + 1);
const b = a.substring(a.lastIndexOf("/"));
return a.replace(b , "");
}
Although it's working as expected but I think there should be a cleaner solution to do this...
As you see I want the string between courseName and the file name at the end of the URl.
I'm not completly sure what you mean by cleaner solution but this is a one-liner line with regex supposing you got the same variable names like in your snippet. Is this what you wanted to achieve? You can trim the last and first character to remove the slashes if needed.
const source = "your majesty from https://example.com/english/courses/frozen_2019/olaf_is_enjoying_living_his_dream_1/voices/references/translate/do%20you%20hear%20that.mp3";
const courseName = "olaf_is_enjoying_living_his_dream_1";
let extracted = source.match("(?<="+courseName+").*\/");
console.log(extracted);
As you see I want the string between courseName and the file name at
the end of the URL.
When manipulating URL strings, it's often a good idea to split the string into an array using:
let myURLArray = myURLString.split('/');
Then, in this situation, you can use:
indexOf()
splice()
join()
to return the section of the URL that you want.
Working Example:
const courseName = "olaf_is_enjoying_living_his_dream_1";
const source = "your majesty from https://example.com/english/courses/frozen_2019/olaf_is_enjoying_living_his_dream_1/voices/references/translate/do%20you%20hear%20that.mp3";
let sourceArray = source.split('/');
let courseNameIndex = sourceArray.indexOf(courseName);
let urlSectionArray = sourceArray.splice((courseNameIndex + 1), ((sourceArray.length - 1) - courseNameIndex - 1));
let urlSection = urlSectionArray.join('/');
console.log(urlSection);
Hi if your source is consinstent with its structure you can split and join the pieces you require.
source.split('/').splice(7,3).join('/')

Substring between two strings?

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"))

Using RegExp to substring a string at the position of a special character

Suppose I have a sting like this: ABC5DEF/G or it might be ABC5DEF-15 or even just ABC5DEF, it could be shorter AB7F, or AB7FG/H.
I need to create a javascript variable that contains the substring only up to the '/' or the '-'. I would really like to use an array of values to break at. I thought maybe to try something like this.
...
var srcMark = array( '/', '-' );
var whereAt = new RegExp(srcMark.join('|')).test.str;
alert("whereAt= "+whereAt);
...
But this returns an error: ReferenceError: Can't find variable: array
I suspect I'm defining my array incorrectly but trying a number of other things I've been no more successful.
What am I doing wrong?
Arrays aren't defined like that in JavaScript, the easiest way to define it would be with:
var srcMark = ['/','-'];
Additionally, test is a function so it must be called as such:
whereAt = new RegExp(srcMark.join('|')).test(str);
Note that test won't actually tell you where, as your variable suggests, it will return true or false. If you want to find where the character is, use String.prototype.search:
str.search(new RegExp(srcMark.join('|'));
Hope that helps.
You need to use the split method:
var srcMark = Array.join(['-','/'],'|'); // "-|/" or
var regEx = new RegExp(srcMark,'g'); // /-|\//g
var substring = "222-22".split(regEx)[0] // "222"
"ABC5DEF/G".split(regEx)[0] // "ABC5DEF"
From whatever i could understand from your question, using this RegExp /[/-]/ in split() function will work.
EDIT:
For splitting the string at all special characters you can use new RegExp(/[^a-zA-Z0-9]/) in split() function.
var arr = "ABC5DEF/G";
var ans = arr.split(/[/-]/);
console.log(ans[0]);
arr = "ABC5DEF-15";
ans = arr.split(/[/-]/);
console.log(ans[0]);
// For all special characters
arr = "AB7FG/H";
ans = arr.split(new RegExp(/[^a-zA-Z0-9]/));
console.log(ans[0]);
You can use regex with String.split.
It will look something like that:
var result = ['ABC5DEF/G',
'ABC5DEF-15',
'ABC5DEF',
'AB7F',
'AB7FG/H'
].map((item) => item.split(/\W+/));
console.log(result);
That will create an Array with all the parts of the string, so each item[0] will contain the text till the / or - or nothing.
If you want the position of the special character (non-alpha-numeric) you can use a Regular Expression that matches any character that is not a word character from the basic Latin alphabet. Equivalent to [^A-Za-z0-9_], that is: \W
var pattern = /\W/;
var text = 'ABC5DEF/G';
var match = pattern.exec(text);
var position = match.index;
console.log('character: ', match[0]);
console.log('position: ', position);

How to use split in javascript

I have a string like
/abc/def/hij/lmn.o // just a raw string for example dont know what would be the content
I want only /abc/def/hij part of string how do I do that.
I tried using .split() but did not get any solution.
If you want to remove the particular string /lmn.o, you can use replace function, like this
console.log(data.replace("/lmn.o", ""));
# /abc/def/hij
If you want to remove the last part after the /, you can do this
console.log("/" + data.split("/").slice(1, -1).join("/"));
# /abc/def/hij
you can do
var str = "/abc/def/hij/lmn.o";
var dirname = str.replace(/\/[^/]+$/, "");
Alternatively:
var dirname = str.split("/").slice(0, -1).join("/");
See the benchmarks
Using javascript
var x = '/abc/def/hij/lmn.o';
var y = x.substring(0,x.lastIndexOf("/"));
console.log(y);
var s= "/abc/def/hij/lmn.o"
var arr= s.split("/");
after this, use
arr.pop();
to remove the last content of the array which would be lmn.o, after which you can use
var new_s= arr.join("/");
to get /abc/def/hij

Categories