RegEx for no whitespace at the beginning and end

后端 未结 14 1171
傲寒
傲寒 2020-11-27 07:20

I want to design an expression for not allowing whitespace at the beginning and at the end of a string, but allowing in the middle of the string.

The regex I\'ve tri

14条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-11-27 07:32

    I found a reliable way to do this is just to specify what you do want to allow for the first character and check the other characters as normal e.g. in JavaScript:

    RegExp("^[a-zA-Z][a-zA-Z- ]*$")
    

    So that expression accepts only a single letter at the start, and then any number of letters, hyphens or spaces thereafter.

提交回复
热议问题