Javascript 匹配字符串中至少 1 个数字和 1 个字符的正则表达式模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7684815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 03:10:42  来源:igfitidea点击:

Regex pattern to match at least 1 number and 1 character in a string

javascriptregexvalidation

提问by OM The Eternity

I have a regex

我有一个正则表达式

/^([a-zA-Z0-9]+)$/

/^([a-zA-Z0-9]+)$/

this just allows only alphanumerics but also if I insert only number(s) or only character(s) then also it accepts it. I want it to work like the field should accept only alphanumeric values but the value must contain at least both 1 character and 1 number.

这只允许只输入字母数字,但如果我只插入数字或只插入字符,那么它也接受它。我希望它像字段应该只接受字母数字值一样工作,但该值必须至少包含 1 个字符和 1 个数字。

回答by phihag

Why not first apply the whole test, and then add individual tests for characters and numbers? Anyway, if you want to do it all in one regexp, use positive lookahead:

为什么不先应用整个测试,然后为字符和数字添加单独的测试?无论如何,如果您想在一个正则表达式中完成所有操作,请使用正向前瞻:

/^(?=.*[0-9])(?=.*[a-zA-Z])([a-zA-Z0-9]+)$/

回答by Rob W

This RE will do:

此 RE 将执行以下操作:

/^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i

Explanation of RE:

RE的解释:

  • Match either of the following:
    1. At least one number, then one letter or
    2. At least one letter, then one number plus
  • Any remaining numbers and letters

  • (?:...)creates an unreferenced group
  • /iis the ignore-caseflag, so that a-z== a-zA-Z.
  • 匹配以下任意一项:
    1. 至少一个数字,然后是一个字母
    2. 至少一个字母,然后一个数字
  • 任何剩余的数字和字母

  • (?:...)创建一个未引用的组
  • /i忽略大小写标志,所以a-z== a-zA-Z

回答by Paul Alan Taylor

I can see that other responders have given you a complete solution. Problem with regexes is that they can be difficult to maintain/understand.

我可以看到其他响应者已经为您提供了完整的解决方案。正则表达式的问题在于它们可能难以维护/理解。

An easier solution would be to retain your existing regex, then create two new regexes to test for your "at least one alphabetic" and "at least one numeric".

一个更简单的解决方案是保留现有的正则表达式,然后创建两个新的正则表达式来测试“至少一个字母”和“至少一个数字”。

So, test for this :-

因此,对此进行测试:-

/^([a-zA-Z0-9]+)$/

Then this :-

然后这个:-

/\d/

Then this :-

然后这个:-

/[A-Z]/i

If your string passes all three regexes, you have the answer you need.

如果您的字符串通过了所有三个正则表达式,您就有了所需的答案。

回答by Neuron

While the accepted answer is correct, I find this regex a lot easier to read:

虽然接受的答案是正确的,但我发现这个正则表达式更容易阅读:

REGEX = "([A-Za-z]+[0-9]|[0-9]+[A-Za-z])[A-Za-z0-9]*"

回答by user2043372

This solution accepts at least 1 number and at least 1 character:

此解决方案接受至少 1 个数字和至少 1 个字符:

[^\w\d]*(([0-9]+.*[A-Za-z]+.*)|[A-Za-z]+.*([0-9]+.*))

回答by bobble bubble

And an idea with a negative check.

还有一个带有否定检查的想法。

/^(?!\d*$|[a-z]*$)[a-z\d]+$/i
  • ^(?!at start look aheadif string does not
  • \d*$contain only digits |or
  • [a-z]*$contain only letters
  • [a-z\d]+$matches one or more letters or digits until $end.
  • ^(?!如果字符串没有则在开始时向前看
  • \d*$只包含数字|
  • [a-z]*$只包含字母
  • [a-z\d]+$匹配一个或多个字母或数字直到$结束。

Have a look at this regex101 demo

看看这个 regex101 演示

(the iflag turns on caseless matching: a-zmatches a-zA-Z)

(该i标志打开无壳匹配a-z匹配a-zA-Z

回答by Pablo Garcia

Maybe a bit late, but this is my RE:

也许有点晚了,但这是我的 RE:

/^(\w*(\d+[a-zA-Z]|[a-zA-Z]+\d)\w*)+$/

/^(\w*(\d+[a-zA-Z]|[a-zA-Z]+\d)\w*)+$/

Explanation:

解释:

\w*-> 0 or more alphanumeric digits, at the beginning

\w*-> 0 个或多个字母数字,在开头

\d+[a-zA-Z]|[a-zA-Z]+\d-> a digit + a letter OR a letter + a digit

\d+[a-zA-Z]|[a-zA-Z]+\d-> 一个数字 + 一个字母或一个字母 + 一个数字

\w*-> 0 or more alphanumeric digits, again

\w*-> 0 或更多字母数字,再次

I hope it was understandable

我希望这是可以理解的

回答by Pratik Butani

The accepted answers is not worked as it is not allow to enter special characters.

接受的答案不起作用,因为它不允许输入特殊字符。

Its worked perfect for me.

它对我来说非常完美。

^(?=.*[0-9])(?=.*[a-zA-Z])(?=\S+$).{6,20}$

^(?=.*[0-9])(?=.*[a-zA-Z])(?=\S+$).{6,20}$

  • one digit must
  • one character must(lower or upper)
  • every other things optional
  • 一位数必须
  • 一个字符必须(小写或大写)
  • 所有其他可选的东西

Thank you.

谢谢你。

回答by will

If you need the digit to be at the end of any word, this worked for me:

如果您需要将数字放在任何单词的末尾,这对我有用:

/\b([a-zA-Z]+[0-9]+)\b/g
  • \b word boundary
  • [a-zA-Z] any letter
  • [0-9] any number
  • "+" unlimited search (show all results)
  • \b 词边界
  • [a-zA-Z] 任意字母
  • [0-9] 任意数字
  • “+”无限搜索(显示所有结果)