I have an array with the following regular expression strings.
var fieldTypes = [
{
'/^[0-9]+$/',
'/^(true|false)$/i',
'/^\\d{4}-\\d{2}-\\d{2}$/',
'/^\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}$/i',
'yyyy-mm-ddThh:mm',
];
Now how can I test whether a string matches one of them?
I'm trying something like this.
var value = 'some string';
if (!value.test(fieldTypes[0])) {
alert('The value is not in the correct format.');
return;
}
But this produces an error that test()
is not a function. Obviously, it can't be called on a string directly. But how do I get a valid regular expression object from a string?
UPDATE:
The suggested answer is completely different. Please read the question before closing it. I have strings. They are coming from another source. It cannot be changed. I quite clearly asked how I can get a valid regular expression from a string. If there's no way, then that's the answer. But telling me to use a regular expression literal is not an answer.