Please provide the following:
- SDK Version: “expo”: “~42.0.1”,
- Platforms(Android/iOS/web/all): andriod vs ios
I’m using the following regex:
// uri = file:///data/user/0/host.exp.exponent/cache/ExperienceData/%2540dedicatedmanagers%252Fmy-app/ImagePicker/eb116538-d9ce-4c75-9c51-d5f45b1318f6.jpg
let extension = uri.match(/\..*?$/i);
I’m trying to pull off the file extension from a uri returned by imagePicker. On iOS I get the dot (.) and the extension. The question mark (?) seems to be doing its job of stopping the any character star (*) from being greedy.
// extension = .jpg
However on Android, Its matching from the first dot (.)
// extension = .exp.exponent/cache/ExperienceData/%2540dedicatedmanagers%252Fmy-app/ImagePicker/eb116538-d9ce-4c75-9c51-d5f45b1318f6.jpg
This reminds me of the joke: (No offense intended)
Some people, when confronted with a problem, think “I know, I’ll use regular expressions.” Now they have two problems.
This regex doesn’t involve letters, so the “i” flag is unnecessary, but should not be a problem, of course.
You could avoid the greedy operator by using this regular expression instead:
let extension = uri.match(/\.[^.]*$/);
although you might want to be a bit more restrictive of the characters you accept in the extension.
By the way, how are you logging the extension?
uri.match() should return an array. Not a string.
You could avoid the regex entirely if you don’t care what characters the extension contains like this:
const extension = uri.slice(uri.lastIndexOf('.') + 1);
Great answer… thanks!
I ended up switching to the expression below, but I like your lastIndexOf way better so I’ll be using it!:
Note that in order to get the period I had to remove the +1 (granted I didn’t specify that in my question)
const extension = uri.slice(uri.lastIndexOf('.') ); // includes the period in the extension
Finally, I am pulling the 0th index off of the regex match to get the extension (I didn’t show it in my question). And, because I use the end of string operator ($) in my regex, it should always only return an array with 1 match/value, if one is found.
Well, I should have paid more attention to the regex!
Unless you did this you’re using JSC.
I’m glad you like it. Just bear in mind that
str.lastIndexOf('.') will return
-1 if there is no
str.slice(-1) will return the last character of the string. So you might want to check if the index is
>= 0 before using it.
Again, great advice. Thank you!
This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.