How to make the JavaScript test go wrong if at least one of the forbidden characters is present?

advertisements

I want test to return false if at least one of the following characters are present: ^/,:<>[email protected]#$%^&()+=?()“|!\[#$-

So I did this:

const pattern = /[^/,:<>[email protected]#$%^&()+=?()“|!\[#$-]/
console.log(pattern.test('fff>'))

However this returns true
Codepen

It should return false because there's at least one of the forbidden characters: <.

How to modify the regex to reflect that?


You should specify the start with ^ and end with $ and add a plus to the class +

Otherwise,

It matches fff and so return true.

If you strict start and end, then it will perform a full test.

const pattern = /^[^/,:<>[email protected]#$%^&()+=?()“|!\[#$-]+$/;
console.log(pattern.test('fff>')); // false
console.log(pattern.test('fff')); // true