最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Regex for six characters with at least one digit - Stack Overflow

programmeradmin7浏览0评论

I am looking for a regex with at least 6 characters (no limit) including at least one digit. No spaces allowed.

I have this this regex:

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

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

I am looking for a regex with at least 6 characters (no limit) including at least one digit. No spaces allowed.

I have this this regex:

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

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

Share Improve this question edited Jun 10, 2015 at 12:01 Wiktor Stribiżew 628k41 gold badges498 silver badges614 bronze badges asked Jun 10, 2015 at 7:36 user2587454user2587454 9131 gold badge20 silver badges44 bronze badges 2
  • Please clarify: Are spaces disallowed thoughout or only in the first 6 characters? – acraig5075 Commented Jun 10, 2015 at 7:54
  • no spaces at all. this is for password input. so 'abc123' is ok but 'abc 123' no. – user2587454 Commented Jun 10, 2015 at 9:30
Add a ment  | 

2 Answers 2

Reset to default 7

a regex with at least 6 characters (no limit) including at least one digit. no spaces allowed.

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

Or

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

See demo

  • ^ Start of string
  • (?=\D*\d) - Must be 1 digit (the lookahead is based on the principle of contrast)
  • \S{6,} - 6 or more non-whitespaces
    OR
    [^ ]{6,} - 6 or more characters other than literal normal space

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

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

I think it should work fine

发布评论

评论列表(0)

  1. 暂无评论