-1

API give url but I want get only specific string in url. For example:

https://firebasestorage.googleapis.com/v0/b/project-xxxx.appspot.com/o/folder_lib%2Fphoto_xxx-xxx-xxx.png?alt=media&token=xxx-xxx-xxx20-5-2016.csv?alt=media&token=b5d45a7f-3ab7-4f9b-b661-3a2187adxxxx

But I only want get: photo_xxx-xxx-xxx.png

How to split url at photo and end at png?

FlutterFirebase
  • 2,163
  • 6
  • 28
  • 60
  • 3
    Does this answer your question? [How to extract a string using JavaScript Regex?](https://stackoverflow.com/questions/1707299/how-to-extract-a-string-using-javascript-regex) – Gildas Nov 24 '20 at 08:36

2 Answers2

0

You can split by the string "%2F", get the last element in the array, split that by "? " and get the first element in that

const splitBySlash = url.split('%2F')
const filePath = splitBySlash[splitBySlash.length - 1]
const splitByQuestion = filePath.split('?')
const file = splitByQuestion[0]
Kevin Izuchukwu
  • 479
  • 3
  • 8
0

learn about regex.

The regex to use is: /photo_(.*).png/;

see below how to use it

var res = "https://firebasestorage.googleapis.com/v0/b/project-xxxx.appspot.com/o/folder_lib%2Fphoto_xxx-xxx-xxx.png?alt=media&token=xxx-xxx-xxx20-5-2016.csv?alt=media&token=b5d45a7f-3ab7-4f9b-b661-3a2187adxxxx".match(/photo_(.*).png/)[0];

console.log(res)

Here's a nice app to practice and test yourself

IsraGab
  • 4,819
  • 3
  • 27
  • 46
  • Thanks for reply! I try this but get error: `TypeError: Cannot read property '0' of null` – FlutterFirebase Nov 23 '20 at 22:54
  • can you please send me the string? - following your example it should work – IsraGab Nov 23 '20 at 22:57
  • Thanks! I am use this string: `https://firebasestorage.googleapis.com/v0/b/project-xxxx.appspot.com/o/folder_lib%2Fphoto_xxx-xxx-xxx.png?alt=media&token=xxx-xxx-xxx20-5-2016.csv?alt=media&token=b5d45a7f-3ab7-4f9b-b661-3a2187adxxxx` – FlutterFirebase Nov 23 '20 at 22:58
  • How are you using it? I updated my answer with the string you just post and it works. click on `run code snippet` it will show you the result – IsraGab Nov 23 '20 at 23:01