JavaScript Regex (string should include only alpha, space, hyphen)

后端 未结 2 1770
情话喂你
情话喂你 2021-02-07 07:47

I\'m trying to create a regex that will match only when the string has anything but alphas, spaces, and hyphens. In other words, the string can only contain letters, spaces, and

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-07 08:19

    If you are looking for a test for validity:

    // from string start to end, only contains '-' "whitespace" or 'a'-'z' 
    someString.match(/^[-\sa-zA-Z]+$/) 
    

    Or the negation:

    // has some invalid character not '-' "whitespace" or 'a'-'z'
    someString.match(/[^-\sa-zA-Z]/) 
    

提交回复
热议问题