正则表达式模式匹配字符串中至少1个数字和1个字符 [英] Regex pattern to match at least 1 number and 1 character in a string

查看:381
本文介绍了正则表达式模式匹配字符串中至少1个数字和1个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有正则表达式


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

这只允许使用字母数字,但如果我只插入数字或只插入字符( s)然后它也接受它。我希望它的工作方式是字段应该只接受字母数字值,但值必须至少包含1个字符和1个数字。

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.

推荐答案

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

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]+)$/

这篇关于正则表达式模式匹配字符串中至少1个数字和1个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆