By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,730 Members | 1,524 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,730 IT Pros & Developers. It's quick & easy.

converting a string to regular expression

P: 15
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.
Nov 14 '06 #1
Share this Question
Share on Google+
2 Replies


P: 15
I want a regular expression such that it will not allow " and ' in a text.
Jan 8 '07 #2

gchq
P: 96
I assume you mean search and replace?

I think what you want is:-

s/and/ /g

(the g is for 'global')
Jan 8 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.