RegEx javascript not matching correctly -
i have small javascript funtcion :
function getfilteredlistlimited(event) { var $source = $(event.target); var $pattern = event.data.pattern; var re = new regexp($pattern, 'i'); if (re.test($source.val())) { console.log('regex match'); } };
the pattern used is:
^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$
which should match email-addresses.
using http://regexpal.com/ can see pattern correct. weird reason script matches @ 4th character after @
abc@abcd should not give match, does. suggestions ?
you need aware of regexp
constructor escaped characters must double-escaped. so, regex string passed regexp constructor should like:
^[a-z0-9._%+-]+@[a-z0-9.-]+\\.[a-z]{2,4}$
the fix can introduced this:
var re = new regexp($pattern.replace(/\\/g, '\\\\'), 'i');
it work if escape symbols used consistently.
Comments
Post a Comment