0

I want to use regex to allow/reject files during fileupload with a specific file type.

This is the working regex /(\.|\/)(xlsx)$/i expected output that I want to achieve.

What I'm trying is instead of hardcode the filetype, I want to pass a variable that holds filetype into the regex.

var fileType = 'xlsx';
var regexText = new RegExp("/(\.|\/)(" + fileType + ")$/i"); 

but the current output of regexText is /\/(.|\/)(xlsx)$\/i/ this is not same as expected output. How do I can get same output as mentioned above by using a variable without hardcode? Is there solution without using the RegExp that can give the same expected output?

Mahesh
  • 1
  • 1
  • 1
    Look at the [RegExp](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp) constructor. You don't need the leading `/`, double escape the backslash and the `/i` flag should go as the second parameter. Note that you can write the alternation as a character class. – The fourth bird Aug 02 '21 at 08:18

1 Answers1

0

try forming string for regex line before calling new RegExp

Alex
  • 44
  • 5