Regular expression for six characters with at least one digit

I'm looking for a regex with at least 6 characters (no limit) including at least one digit. Without spaces.

I have this regex:

^(?=.*\d).{4,8}$

      

However, I don't want to limit to 8 characters.

+3


source to share


1 answer


regular expression with at least 6 characters (no limit), including at least one digit. without spaces.

^(?=\D*\d)\S{6,}$

      

Or

^(?=\D*\d)[^ ]{6,}$

      



Watch the demo

  • ^

    Start of line
  • (?=\D*\d)

    - must be 1 digit (lookahead is based on contrast principle)
  • \S{6,}

    - 6 or more non-spaces
    OR
    [^ ]{6,}

    - 6 or more characters other than normal space

To enable regex to match more than 6 characters, you only need to adjust the quantifier. See more about limiting quantifiers here .

+5


source







All Articles