-2

here i am trying to validate the SVG image mime type. Even I am passing valid content-type also it fails. Can anyone suggest to me what's wrong with this regex?

const mimetypes = /image\/png|image\/jpeg|imagesvg+xml|image\/gif|image\/svg+xml/;

var result = mimetypes.test('image/svg+xml')

console.log(result)

4 Answers4

2

You are checking a fixed string against a bunch of fixed strings. You don't need regex at all.

const mimetypes = ['image/png', 'image/jpeg', 'image/svg+xml', 'image/gif'];

var result = mimetypes.includes('image/svg+xml')
Tomalak
  • 332,285
  • 67
  • 532
  • 628
0

You must scape the + symbol as it has meaning in regex:

const mimetypes = /image\/png|image\/jpeg|imagesvg\+xml|image\/gif|image\/svg\+xml/;

var result = mimetypes.test('image/svg+xml')

console.log(result)
Tomalak
  • 332,285
  • 67
  • 532
  • 628
jeprubio
  • 17,312
  • 5
  • 45
  • 56
0

You should escape + as well, it's a special char for regexes

const mimetypes = /image\/png|image\/jpeg|imagesvg\+xml|image\/gif|image\/svg\+xml/;

var result = mimetypes.test('image/svg+xml')

console.log(result)
Luca Faggianelli
  • 2,032
  • 20
  • 23
0

+ is a quantifier in regex so you also have to escape the + sign

const mimetypes = /image\/png|image\/jpeg|imagesvg\+xml|image\/gif|image\/svg\+xml/;

var result = mimetypes.test('image/svg+xml')

console.log(result)
Ankit
  • 604
  • 6
  • 13