I need a regex for javascript that includes a-z, A-Z, and spaces
我需要一个包含a-z、a-z和空格的javascript正则表达式
For example, the string "Bob says Hi" would be accepted, but not "There were 4 clowns"
例如,字符串“Bob says Hi”将被接受,而不是“There were 4小丑”
The closest I've gotten is /^[a-zA-Z]+$/
which includes a-z and A-Z but not spaces.
最近的我已经是/ ^[a-zA-Z]+ /美元,包括a - z、a - z但不空格。
2 个解决方案
#1
32
/^[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个。
#2
11
You can use this to match a sequence of a-z, A-Z and spaces:
你可以用它来匹配a-z、a-z和空格的序列:
/[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:
如果你想知道一个字符串是否完全由a-z, a-z和空格组成,那么你可以这样使用:
/^[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.
对于其他regex符号,internet上有大量的引用。这是我经常收藏和查看的一个: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。
#1
32
/^[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个。
#2
11
You can use this to match a sequence of a-z, A-Z and spaces:
你可以用它来匹配a-z、a-z和空格的序列:
/[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:
如果你想知道一个字符串是否完全由a-z, a-z和空格组成,那么你可以这样使用:
/^[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.
对于其他regex符号,internet上有大量的引用。这是我经常收藏和查看的一个: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。