用于字母字符和空格的 Javascript 正则表达式?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8059370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Javascript regex for alphabetic characters and spaces?
提问by DontTurnAround
I need a regex for javascript that includes a-z, A-Z, and spaces
我需要一个包含 az、AZ 和空格的 javascript 正则表达式
For example, the string "Bob says Hi" would be accepted, but not "There were 4 clowns"
例如,字符串“Bob say Hi”将被接受,但不能接受“There was 4 clowns”
The closest I've gotten is /^[a-zA-Z]+$/
which includes a-z and A-Z but not spaces.
我得到的最接近的是/^[a-zA-Z]+$/
包括 az 和 AZ 但不包括空格。
回答by chown
/^[A-Za-z ]+$/
or /^[A-Za-z\s]+$/
/^[A-Za-z ]+$/
或者 /^[A-Za-z\s]+$/
More good stuff here:
http://www.regular-expressions.info/javascript.html
这里有更多好东西:http:
//www.regular-expressions.info/javascript.html
or just /\w+$/
if you also want 0-9 and underscores (\w stands for "word character", usually [A-Za-z0-9_]
). But your recent edit indicates that you dont want 0-9, so use one of the first 2 above.
或者只是/\w+$/
如果您还需要 0-9 和下划线(\w 代表“单词字符”,通常为[A-Za-z0-9_]
)。 但是您最近的编辑表明您不想要 0-9,因此请使用上面的前 2 个之一。
回答by jfriend00
You can use this to match a sequence of a-z, A-Z and spaces:
您可以使用它来匹配一系列 az、AZ 和空格:
/[a-zA-Z ]+/
If you're trying to see if a string consists entirely of a-z, A-Z and spaces, then you can use this:
如果您想查看字符串是否完全由 az、AZ 和空格组成,则可以使用以下命令:
/^[a-zA-Z ]+$/
Demo and tester here: http://jsfiddle.net/jfriend00/mQhga/.
演示和测试人员在这里:http: //jsfiddle.net/jfriend00/mQhga/。
For other regex symbols, there are tons of references on the internet. This is the one I have bookmarked and look at regularly: http://www.javascriptkit.com/javatutors/redev2.shtml.
对于其他正则表达式符号,互联网上有大量参考资料。这是我已添加书签并定期查看的一个:http: //www.javascriptkit.com/javatutors/redev2.shtml。
And, you can practice in an online tool here: http://www.regular-expressions.info/javascriptexample.html.
而且,您可以在此处使用在线工具进行练习:http: //www.regular-expressions.info/javascriptexample.html。