I have an URL of an images like this:
This URL contains the image name that in this particualr case it is:gatto.jpg
What could be a smart way to extract the name from a string like the previous one representing my URL.
Consider the case that this URL is defined itno a string named url
let url = "https://firebasestorage.googleapis.com/v0/b/my-project/o/attachments%2F1610905935653_gatto.jpg?alt=media&token=a89b3cbb-d1cd-4310-b2f3-23395db50033"
So I have to extract what is after the _ character and what it is before the ? character.
What can be a smart solution to achieve this task?
Advertisement
Answer
Since I’m not too familiar with RegEx I would just split the string twice, since you have only one “_” and one “?” In your URL, I would first split the URL based on “?” and then take the first element and split that again based on “_”
var url = "https://firebasestorage.googleapis.com/v0/b/my-project/o/attachments%2F1610905935653_gatto.jpg?alt=media&token=a89b3cbb-d1cd-4310-b2f3-23395db50033"; var filename = url.split("?")[0].split("_")[1];
Of course you can always look up the RegEx for that as well.