【问题标题】:Regex to accept Min 6 characters, at least one letter and at least one of the following: 0123456789-.@_正则表达式接受最少 6 个字符、至少一个字母和至少以下之一:0123456789-.@_
【发布时间】:2018-07-11 09:15:54
【问题描述】:

我希望接受一个正则表达式:

  1. 最少 6 个字符
  2. 至少一个字母
  3. 以下至少一项:0123456789-.@_

我试过了:

 ^(?=.*[A-Za-z])(?=.*\d)(?=.*[._@-])[A-Za-z\d._@-]{1,40}$ 

【问题讨论】:

  • 到目前为止你有没有尝试过? StackOverflow 不是免费的代码编写服务,希望您能try to solve your own problem first。请更新您的问题以显示您已经尝试过的内容,并在minimal, complete, and verifiable example 中显示您面临的具体问题。欲了解更多信息,请参阅how to ask a good question,并拨打tour of the site
  • 我试过这个正则表达式,但它不能解决我的问题。 ^(?=.*[A-Za-z])(?=.*\d)(?=.*[._@-])[A-Za-z\d._@-]{1, 40}$
  • 如果你的最小长度是 6,你会认为你的量词是 {6,40}
  • 您的正则表达式将需要至少 1 个 a-zA-Z ...至少一个数字和至少一个其他字符...尝试结合数字和特殊字符前瞻
  • 其他字符可以是任何东西吗?

标签: javascript regex html regex-group


【解决方案1】:

你可以试试这个:

^(?=.*[a-zA-Z])(?=.*[\d.@_-]).{6,}$

Regex Demo 1

或者,如果您不想使用任何字符,而只需要提到的字符来构成您的整个字符串,那么您也可以尝试以下方法:

^(?=.*[a-zA-Z])(?=.*[\d.@_-])[a-zA-Z\d.@_-]{6,40}$

Regex Demo 2

演示 1 的示例源代码:

const regex = /^(?=.*[a-zA-Z])(?=.*[\d.@_-]).{6,}$/g;
console.log(regex.test(`0a123456789-.@_`));
console.log(regex.test(`abcdefghijklmn`));
console.log(regex.test(`0a123456789`));
console.log(regex.test(`0a12`));

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2013-05-29
  • 1970-01-01
  • 1970-01-01
  • 2011-07-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多