Regex to validate names

前端 未结 6 1822
一个人的身影
一个人的身影 2021-01-19 12:04

I would like to create a regex which validates a name of a person. These should be allowed:

  • Letters (uppercase and lowercase)
  • -
6条回答
  •  长发绾君心
    2021-01-19 12:25

    ^.+$
    

    Checked @jensgram answer, but that regex only accepts all strings, so it doesn't solve problem, because string needs to be name, in this case it can be anything.

    ^[A-Z][a-z]+$
    

    My regex only accepts string where first char is uppercase and following chars are letters in lowercase. Also looking through other answers, this seems to be shortest regex and also simpliest.

提交回复
热议问题