var str = "[email protected]"; // true but coming false
var str1 = "[email protected]";
var str2 = "[email protected]";
var str3 = "[email protected]";
var patt = new RegExp("[a-z0-9._%+-]+@[a-z0-9.-]+?[\.com]?[\.org]?[\.co.uk]?[\.org.uk]$");
console.log( str + " is " + patt.test(str));
console.log( str1 + " is " + patt.test(str1));
console.log( str2 + " is " + patt.test(str2));
console.log( str3 + " is " + patt.test(str3));
Can anyone tell me what is the mistake, my .com example is not working properly
You need
^anchor at the start of the pattern since you need both^and$to make the pattern match the entire string.So you need to use
See the regex demo.
If you need to make it case insensitive, add
iflag,