Allow only certain character in string. Javascript

Viewed 2013

I have no idea, why this simple code is not working. I am planning to match a string against the allowed pattern. The string should ONLY have a-z, A-Z, 0-9, _ (underscore), . (dot) , - (hiphen).

Below is code:

var profileIDPattern = /[a-zA-Z0-9_.-]./;
var str = 'Heman%t';
console.log('hemant',profileIDPattern.test(str));

The code logs 'true' for below string, although these string DOES NOT match the pattern.

'Heman%t' -> true
'#Hemant$' -> true

I dont know what is the problem.

2 Answers
Related