Reputation: 33
I'm doing some work with a chrome extension that needs to use the google chrome results URLs, which are redirects, and convert those to ordinary URLs for use. Examples:
What I want: https://www.google.com
I've tried using string.split().pop(), but that just used EVERYTHING after the inputted text. For a basic example, I want a way to reliably turn testHItestBYE into HIBYE. What would be the best way to do this?
Upvotes: 0
Views: 1150
Reputation: 1061
Redirect URLs seem to have target URL in the url
parameter, so we can split the string using '&url='
as delimiter, pop the last element, split it using '&'
as the delimiter, get the first element. It is URL-encoded, so we'll have to decode it.
var findURL = function findURL(url){
return decodeURIComponent(url.split('&url=').pop().split('&')[0]);
}
//example call
result = findURL( 'https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwi6w8WR4ZzSAhUZHGMKHRntAv0QFggaMAA&url=https%3A%2F%2Fwww.google.com%2F&usg=AFQjCNFePWT_Lkni-D9ikX7wC3eYuDMQYQ&sig2=gPD7xuE6nstkxWFhSh2QLQ&bvm=bv.147448319,d.cGw' );
console.log(result);
There are other ways to do it.
Upvotes: 1