-1

Let's say I have a filename 'file1.txt' How to check if a filename has either of these extensions (jpg, txt, mp4)?

If I add 'txt' first then it returns 1 (true)

var test = 'file1.txt';
test.substring(test.lastIndexOf(".")+1) === "txt"|"jpg"|"mp4"

and if I add 'txt' in the last like this "jpg"|"mp4"|"txt" then it returns 0.

I want to simply check that if a filename has any extension or not.

Jaz
  • 135
  • 4
  • 16

1 Answers1

2

I would use test here with a regex pattern:

console.log(/\.(txt|jpg|mp4)$/.test('file1.txt'));

One coding advantage here to the regex approach is we can just list all extensions we want to match in an alteration. In your current attempt, we can't compare a string to an alternation with ===.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360