最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reg. Expression checks first letter of a string - Stack Overflow

programmeradmin3浏览0评论

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?

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?

Share Improve this question edited Mar 9, 2011 at 5:10 Jon 4,9453 gold badges29 silver badges38 bronze badges asked Mar 9, 2011 at 4:53 AadiAadi 7,10928 gold badges102 silver badges148 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 13

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');

Try the following:

'/^[a-zA-Z].*$/'

which checks if the first letter is in the alphabet and allows any character after that.

I was able to single out the first letter with this /^[a-zA-Z].*$/ from one of the above answers, but I didn't need the .\*$ so I had /^[A-J]/g (working on my own assignment hence a-j).

发布评论

评论列表(0)

  1. 暂无评论