javascript - Can anyone explain the difference between these two regular expressions? -
/^[a-za-z]+$/
vs
/[^a-za-z]+$/
the ^
@ start of expression means "anchor @ beginning of string".
the ^
inside character class []
expression means negate
.
so /^[a-za-z]$/
matches string consists entirely (from beginning end) of upper case , lower case alphabetic characters, while /[^a-za-z]$/
matches "end of string not consist of alphabetic characters" (for example, numbers @ end of string).
this string -- matches neither (contains non alphabetic, doesn't end in it) number: 123 -- second expression matches ': 123' (string ends in non-alphabetic characters) -- first expression matches 'this' (string contains alphabetic characters)
Comments
Post a Comment