Need a regular expression - disallow all zeros

Just have a negative lookahead like this to disallow all 0s:

/^(?!0{6})[A-Z0-9][0-9]{5}$/

You can use a (?!0+$) negative lookahead to avoid matching a string that only contains 1 or more zeros:

/^(?!0+$)[A-Z0-9][0-9]{5}$/
  ^^^^^^^

See the regex demo. This approach lets you just copy/paste the lookahead after ^ and not worry about how many chars the consuming part matches.

Details

  • ^ - start of a string
  • (?!0+$) - a negative lookahead that fails the match if there are 1 or more 0 chars up to the end of the string ($)
  • [A-Z0-9] - an uppercase ASCII letter or a digit
  • [0-9]{5} - five digits
  • $ - end of string.

Tags:

Regex