且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

JavaScript正则表达式(字符串应仅包含alpha,空格,连字符)

更新时间:2023-02-26 14:06:24

如果您正在寻找有效性测试:

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]+$/) 

或否定:

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