Reg. Expression checks the first letter of a string

I would like to check if the first character of the string is a letter or not. My regex is:

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

This does not work. What is wrong with him?

+3
source share
2 answers

Try the following:

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

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

+2
source

Your expression does not need. * and should not have $

'/^([a-zA-Z])/'

In fact, if you don’t need to know what this letter is, you can go even easier:

'/^[a-zA-Z]/'

// These expressions are true
/^[a-zA-Z]/.test("Sample text")

var re = new RegExp('^[a-zA-Z]');
re.test('Sample text');
+13
source

All Articles