I'm looking to do a name check using regex in javascript.
I came up with this, but feel it's incorrect. Any advice on how to have it foolproof?
^[A-Za-z0-9]+[A-Za-z0-9_@-.]{30}
You are quite close, here is the corrected regex:
/^[a-z0-9][\w@.-]{0,30}$/i
I applied the folloing changes:
$
anchor to the end a-zA-Z0-9_
with \\w
and moved -
to the end of the character class {30}
to {0,30}
See the Regex101-Demo with some unit tests.
^[A-Za-z0-9][A-Za-z0-9_@.-]{0,30}
-
at the and of brackets, so it won't be used as a range {,30}
+
after first character (without it you would get >31 chars) You can always check your patterns at www.regexr.com
\\-
. {30}
to {0,30}
because it doesn't need to be exactly 31 characters long AZ
parts $
Result looks like this:
/[a-z0-9][a-z0-9@\-._]{0,30}/i
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.