javascript - How can I escape characters inside of this regular expression? -
i have function validate email address. jslint gives error regular expression complaining of characters being un-escaped.
is there way correctly escape them?
var validateemail = function(elementvalue){     var emailpattern = /^[a-za-z0-9._-]+@[a-za-z0-9.-]+\.[a-za-z]{2,4}$/;     return emailpattern.test(elementvalue); };      
the regular expression using is valid. guess jslint complains missing escape sign in front of - in character classes:
/^[a-za-z0-9._\-]+@[a-za-z0-9.\-]+\.[a-za-z]{2,4}$/   escaping - inside character class required if not @ begin or end of character class or if not denoting range when used between 2 characters.
here examples valid:
/[-xyz]/    // "-", "x", "y", "z" /[xyz-]/    // same above /[-]/       // "-" /[a-z-]/    // "a"-"z", "-" /[a-b-c]/   // "a"-"b", "-", "c"      
Comments
Post a Comment