JavaScript正则表达式(字符串应仅包含alpha,空格,连字符) [英] JavaScript Regex (string should include only alpha, space, hyphen)

查看:77
本文介绍了JavaScript正则表达式(字符串应仅包含alpha,空格,连字符)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个正则表达式,仅当字符串中包含除alpha,空格和连字符之外的任何内容时才会匹配。换句话说,字符串只能包含字母,空格和连字符。

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 hyphens.

推荐答案

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

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

这篇关于JavaScript正则表达式(字符串应仅包含alpha,空格,连字符)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆