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 badges3 Answers
Reset to default 13Your 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).