I'm validating an email address, and I use a regular expression that I saw here :
/^[-\w.%+]{1,64}@(?:[A-Z0-9-]{1,63}\.){1,125}[A-Z]{2,63}$/i
My problem is that I would like to know what is the function of each value inside the square brackets in:
[-\w.%+]{1,64}
It is not clear to me what each thing means in that expression.
It's actually quite simple, but it's confusing because some symbols used here as literals outside the bracket are special.
[-\w.%+]{1,64}
: It is the "user name" of an e-mail[]
Bracket, alternative, matches any of the symbols defined within-
The literal-
(because it goes to the beginning, if it were between two other symbols it would represent the range between them:0-9
).\w
Classw
, which represents "symbols that go into words"; letters and numbers:A-Za-z0-9
and underscore_
.
The literal.
, which is a valid symbol in the email. Outside the bracket represents a wildcard, but inside the bracket only represents the literal.%
The literal%
, which is a valid symbol in the email.+
The literal+
, which is a valid symbol in the email. Outside the bracket it would represent "one or more repetitions", but inside the bracket it only represents the literal.{1,64}
From 1 to 64 repetitions.Example: https://regex101.com/r/eVaDQ3/1