I want to allow only specific special characters like in a text like ?*=.@#$%!^":,:;<'>+-_.
I have tried the below :
pattern = new RegExp(/^(?=.*?[?*=.@#$%!^":,:;<'>+-_])/);
It does not seems to work and seems to pass even if i am entering a special character other than the ones specified above. Am i missing something here?
Eg :
var sampleText: string = "TestSample&123"
The above example should throw me an error and fail since i have not used any of the special character which is specified in the pattern.
>Solution :
Based on the charset you want and the approach from this answer here about exclusive sets in Regex, you should be able to do something like this:
const testPattern = /^[?*=.@#$%!^":,;<'>+\-_]+$/;
console.log(testPattern.test('?*+@!')); // passes
console.log(testPattern.test('TestSample&123')); // fails