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

Popular posts from this blog

c++ - Convert big endian to little endian when reading from a binary file -

C#: Application without a window or taskbar item (background app) that can still use Console.WriteLine() -

unicode - Are email addresses allowed to contain non-alphanumeric characters? -