How do I create a regex that matches all alphanumerics without a number at the beginning?
Right now I have "^[0-9][a-zA-Z0-9_]"
For example, 1ab would not match, ab1 would match, 1_bc would not match, bc_1 would match.
To check if a string does not start with specific characters using a regular expression, use the test() function and negate it. Make sure your regular expression starts with ^ , which is a special character that represents the start of the string.
Definition and Usage. The ?! n quantifier matches any string that is not followed by a specific string n. Tip: Use the ?= n quantifier to match any string that IS followed by a specific string n.
\s stands for “whitespace character”. Again, which characters this actually includes, depends on the regex flavor. In all flavors discussed in this tutorial, it includes [ \t\r\n\f]. That is: \s matches a space, a tab, a carriage return, a line feed, or a form feed.
There are three things wrong with what you've written.
First, to negate a character class, you put the ^
inside the brackets, not before them. ^[0-9]
means "any digit, at the start of the string"; [^0-9]
means "anything except a digit".
Second, [^0-9]
will match anything that isn't a digit, not just letters and underscores. You really want to say that the first character "is not a digit, but is a digit, letter, or underscore", right? While it isn't impossible to say that, it's a lot easier to just merge that into "is a letter or underscore".
Also, you forgot to repeat the last character set. As-is, you're matching exactly two characters, so b1
will work, but b12
will not.
So:
[a-zA-Z_][a-zA-Z0-9_]*
Debuggex Demo
In others words: one letter or underscore, followed by zero or more letters, digits, or underscores.
I'm not entirely sure this is what you actually want, at least if the regex is your whole parser. For example, in foo-bar
, do you want the bar
to get matched? If so, in 123spam
, do you want the spam
to get matched? But it's what you were trying to write.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With