I want to validate a string to meet the following conditions:
I have the following regular expression that gets everything except the all zeros part. Is there a way to disallow all zeros?
^[A-Z0-9][0-9]{5}$
Is the only way to do this to check the regex (and allow "000000") but then check specifically that it's not "000000"?
Thanks.
To match any character except a list of excluded characters, put the excluded charaters between [^ and ] . The caret ^ must immediately follow the [ or else it stands for just itself.
In this case, [0-9]+ matches one or more digits. A regex may match a portion of the input (i.e., substring) or the entire input. In fact, it could match zero or more substrings of the input (with global modifier). This regex matches any numeric substring (of digits 0 to 9) of the input.
A regular expression followed by an asterisk ( * ) matches zero or more occurrences of the regular expression. If there is any choice, the first matching string in a line is used.
Throw in an * (asterisk), and it will match everything. Read more. \s (whitespace metacharacter) will match any whitespace character (space; tab; line break; ...), and \S (opposite of \s ) will match anything that is not a whitespace character.
Just have a negative lookahead like this to disallow all 0s:
/^(?!0{6})[A-Z0-9][0-9]{5}$/
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