I am attempting to extract the file extension from a filename. The filename may incorporate special characters such as "#, @, ., _, (), etc. For example:
var file1 = "fake.der"
var file2 = "fake.1.der"
var file3 = "fake_test.3.der"
In the scenarios above, I aim to only retrieve the extension "der" from each filename. I have attempted the following:
file1.split(".")[1] // works correctly
file2.split(".")[1] // returns 1 - incorrect, but file2.split(".")[2] gives the correct result
file3.split(".")[1] // returns 3 - incorrect
Given the varying filenames, I wish to avoid hardcoding the .split(".")[1]
index and having to adjust it to .split(".")[2]
or other values for different filenames.
Is there a more effective approach to ensure that regardless of the number of dots in the filename, I always obtain only the extension as output?
Thank you!