Regex is the bane of my existence. I've done plenty tutorials, but the rules never stick, and when I look them up they seem to conflict. Anyways enough of my whining. Could someone tell me why this regex doesn't exclude hyphens or brackets:
/^[A-Za-z_][A-Za-z\d_]*/
The way I understand it (or at least what I'm trying to do), the ^
character dictates that the regex should start with the next thing on the list That means the regex should start with [A-Za-z_]
or any character a-z and A-Z as well as and underscore _
. Then the string can have anything that includes [A-Za-z\d_]
which is any alphanumeric character and an underscore. Then I use the *
to say that the string can have any number of what was presented previously (any alphanumeric character plus underscore). At no point to I specify a bracket [
or a hyphen -
. Why does this expression not exclude these characters
Extra info I'm verifying this with javascript:
function variableName(name) {
const reg = RegExp("^[A-Za-z_][A-Za-z\d_]*")
return reg.test(name)
}
function variableName("va[riable0") // returns true should be false