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

后端 未结 9 2554
既然无缘
既然无缘 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:52

    This RE will do:

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

    Explanation of 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
    • /i is the ignore-case flag, so that a-z == a-zA-Z.

提交回复
热议问题