What's the cleanest way to extract fcerdak.jpg in this string

Hi guys. Whats the cleanest way to extract fcerdak.jpg in this string
https://firebasestorage.google ... xx123
You already invited:

Bobby

Upvotes from:

thanks for invitation,have you thought about something like this?
var str = "[url=https://firebasestorage.googleapis.com/v0/b/ng-house.appspot.com/o/current-members-logo%2fcerdak.jpg?alt=media&token=123xxx123";]https://firebasestorage.google ... 3B%3B[/url] 
var partial = str.match(/%[0-9A-z]+.[A-z]{3,5}?/g);
var result = partial[0].substring(1);
console.log(result);
 
 

Ezra - Sql

Upvotes from:

you probably don't want to extract the f in fcerdak.jpg, actually it is part of current-members-logo%2fcerdak.jpg, where %2f is a hexadecimal URL encoded character, if you check for a ASCII reference you'll realize that %2f maps to the forward slash unicode character /. So, why is it encoded? Because probably it's not part of the URL, it is actually part of the file name (current-members-logo/cerdak.jpg).

So first I would decode the URL using the global decodeURIComponent function. Then I would create a URL object using the global URL constructor, so now I could get the pathname from the instantiated URL object. Since the pathname would be /v0/b/ng-house.appspot.com/o/current-members-logo/cerdak.jpg we can split it in the forward slash, take the last argument and remove the extension.

Here's the code:
 
const url = 'https://firebasestorage.googleapis.com/v0/b/ng-house.appspot.com/o/current-members-logo%2fcerdak.jpg?alt=media&token=123xxx123'
const decodedURL = decodeURIComponent(url)
const { pathname } = new URL(decodedURL)

const last = x => x[x.length - 1]

const imageName = last(pathname.split('/')).replace('.jpg', '')
// if the extension is not always .jpg, use this regex instead: /\.\w+$/

console.log(imageName) // cerdak

If you wanna answer this question please Login or Register