regex to check the string contains only letter and numbers but not only numbers

后端 未结 10 1469
温柔的废话
温柔的废话 2021-02-06 11:31

I need a help with regex which checks the string contains only letter and numbers but not only numbers

Valid

* letters
* 1wret
* 0123chars
* chars0123
*          


        
10条回答
  •  猫巷女王i
    2021-02-06 12:00

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

    • (?=.*[a-z]) positive lookahead to make sure that there is at least one letter in the string (but doesn't consume any characters - gives up the matched characters as soon as it returns (in this case, as soon as it finds a letter)).
    • [a-zA-Z0-9]+ make sure string contains only alphanumeric characters.
    • ^ and $ are start and end of string delimiters.

提交回复
热议问题