Regex to allow single hyphen and underscore but not at beginning or end of string

前端 未结 2 689
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-20 15:51

I have this Regex I modified to allow underscores, hyphen, letters and numbers. I am trying to modify it further so that it has the following properties:

  1. Allow
2条回答
  •  北荒
    北荒 (楼主)
    2021-01-20 16:07

    ^([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9_-]*[a-zA-Z0-9])$
    

    Either one of the three possibilities:

    • [a-zA-Z0-9]
    • [a-zA-Z0-9][a-zA-Z0-9]
    • [a-zA-Z0-9][a-zA-Z0-9_-]*[a-zA-Z0-9]

提交回复
热议问题