I am trying to do something I thought would be pretty easy to do, which is to restrict a string to certain characters by matching a regular expression.
var value = 'FailureStr1ng';
var type = 'ALPHA';
var regex = null;
switch(type) {
case 'ALPHA':
regex = '^[a-zA-Z]+$';
break;
case 'NUMERIC':
regex = '^[0-9]+$';
break;
case 'ALPHANUMERIC':
regex = '^[a-zA-Z0-9]+$';
break;
}
return value.match(regex);
For some reason, when using the match it always returns null
. Is there a way to fix this, or a better method to do this?
Note: The code here is a snippet of much larger code, and in turn the value and type variable are usually defined by another method.
1
in the string so it does not match. Changing it to a valid string returns that string. jsfiddle.net/GLVAj