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

后端 未结 2 1768
情话喂你
情话喂你 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:17
    if(someString.match(/[a-z -]+/i){
        // it's valid
    }
    
    0 讨论(0)
  • 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]/) 
    
    0 讨论(0)
提交回复
热议问题