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

后端 未结 9 2615
既然无缘
既然无缘 2020-11-22 13:22

I have a regex

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

this just allows only alphanumerics but also if I insert only number(s) or only

9条回答
  •  暗喜
    暗喜 (楼主)
    2020-11-22 13:59

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

提交回复
热议问题