I am trying to make my regular expression generic by using parameters. However, the RegExp does not work when I use variables:
The below regular expression works properly where I limit my input to 7 characters.
var specialCharactersValidation = new RegExp(/^[a-zA-Z0-9]{7}$/);
But when I make it generic:
var characterCount = parseInt(attrs.limitCharacterCount); // Value is 7 and type is int
console.log(characterCount); // Value is 7
var specialCharactersValidation = new RegExp(/^[a-zA-Z0-9]{characterCount}$/);
This is the console for the above code. The regex does not compile with the character count.
7
requestShipmentDirectives.js:73 /^[a-zA-Z0-9]{characterCount}$/ false
This does not work. And neither does the below one:
var characterCount = parseInt(attrs.limitCharacterCount); // Value is 7 and type is int
console.log(characterCount); // Value is 7
var specialCharactersValidation = new RegExp("/^[a-zA-Z0-9]{"+characterCount+"}$/");
This is the console for above code:
7
9requestShipmentDirectives.js:73 /\/^[a-zA-Z0-9]{7}$\// false
RegExp compiles but never works.
The output is always false.
Is there anything missing?