javascript 注册。表达式检查字符串的第一个字母
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5241507/
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
Reg. Expression checks first letter of a string
提问by Aadi
I would like to check if the first character of a string is a letter or not. My regular expression is:
我想检查字符串的第一个字符是否是字母。我的正则表达式是:
'/^([a-zA-Z.*])$/'
This is not working. What's wrong with it?
这是行不通的。它出什么问题了?
采纳答案by Peter C
Try the following:
请尝试以下操作:
'/^[a-zA-Z].*$/'
which checks if the first letter is in the alphabet and allows any character after that.
它检查第一个字母是否在字母表中并允许其后的任何字符。
回答by Dimitry
Your expression does not need .* nor should it have the $
你的表达式不需要 .* 也不应该有 $
'/^([a-zA-Z])/'
In fact, if you don't need to know what this letter is, you could go even simpler:
事实上,如果你不需要知道这封信是什么,你可以更简单:
'/^[a-zA-Z]/'
// These expressions are true
/^[a-zA-Z]/.test("Sample text")
var re = new RegExp('^[a-zA-Z]');
re.test('Sample text');

