converting a string to regular expression
I have to generate a regular expression dynamically to check the file extensions. The file extension types are stored in a model data which are are to be fetched in form of a string and set into an hidden variable.
<input type="hidden" id="extension" value="exe|doc">
In javascript it is used as
var ext = document.getElementById("extension").value;
var regEx = /^.+\.(ext)$/i;
This does not replace the ext value "exe|doc" in the regEx .
If I'm using
var regEx = "/^.+\.("+ext+")$/i;
Then it is giving regEx = /^.+\.(exe|doc)$/i; but regEx is in string format.
So it is not working with "path.search(regEx) != -1"
where path contains the name of the file.
So can anyone tell me how to generate a regular expression dynamically or say convert a string to regular expression.
|