admin管理员组

文章数量:1287967

I am learning javascript and Regex and trying to validate a input if the first letter in the string is in uppercase.

my current Regex is : var patt1=/[A-Z]{4}$/;

but this is validating the string if all the four letters are in uppercase not the first letter only.

Any idea how to make it work.

Note : I am also checking the input should be 4 letter long.

I am learning javascript and Regex and trying to validate a input if the first letter in the string is in uppercase.

my current Regex is : var patt1=/[A-Z]{4}$/;

but this is validating the string if all the four letters are in uppercase not the first letter only.

Any idea how to make it work.

Note : I am also checking the input should be 4 letter long.

Share Improve this question edited Aug 25, 2013 at 6:38 Sunny Soni asked Aug 25, 2013 at 6:31 Sunny SoniSunny Soni 1822 silver badges11 bronze badges 5
  • 4 There is enormous results you will get, when you google your title. – Rohit Jain Commented Aug 25, 2013 at 6:32
  • Where does the 4 e from ? Did you want to validate the length of the string ? – Denys Séguret Commented Aug 25, 2013 at 6:35
  • @RohitJain How to google? – temporary_user_name Commented Aug 25, 2013 at 6:40
  • @Aerovistae. Jesus Christ. :-\ – Rohit Jain Commented Aug 25, 2013 at 6:41
  • 2 Maybe I should google it – temporary_user_name Commented Aug 25, 2013 at 6:41
Add a ment  | 

2 Answers 2

Reset to default 5

Provided that by "uppercase" you mean a letter in A-Z, then you may use

var isFirstLetterUppercase = /^[A-Z]/.test(yourString);

The idea here (and in similar cases) is to use ^ which matches the start of the string.

If you also want to check the length of the string, you may use :

var isOK = /^[A-Z].{3}$/.test(yourString);

Here, .{3} means 3 characters and $ is the end of the string.

The problem with using an A-Z test in a regular expression is that A-Z are not the only uppercase letters.

Consider the city of Überlingen in Germany. The first letter certainly is uppercase, but it is not in the range A to Z. Try it in the JavaScript console:

/^[A-Z]/.test('Überlingen');  // logs false - oops!

Now here is where it gets a bit tricky. What exactly does it mean for a letter to be uppercase? In English it's simple: A-Z vs. a-z. In German, Ü (for example) is uppercase and ü is lowercase. For languages like these that have both uppercase and lowercase characters, you can test if a character is uppercase by converting it to lowercase with the .toLowerCase() method and paring that with the original. If they are different, the original was uppercase. If they are the same, the original was either a lowercase character or a character that doesn't have uppercase and lowercase versions (e.g. a number or punctuation mark).

// 'char' is a string containing a single character
function isUpperCase( char ) {
    return char !== char.toLowerCase();
}

Now you can test if the first character of a string is uppercase by extracting that character with .charAt() and calling isUpperCase():

function beginsWithUpperCase( string ) {
    return isUpperCase( string.charAt(0) );
}

This works correctly for the German city:

beginsWithUpperCase( 'Überlingen' );  // logs `true`.

And now, since we're not using a regular expression at all, if you want to check the string length, merely use the .length property:

function fourCharactersWithFirstUpperCase( string ) {
    return string.length === 4  &&  beginsWithUpperCase( string );
}

fourCharactersWithFirstUpperCase( 'über' );  // logs false
fourCharactersWithFirstUpperCase( 'Über' );  // logs true
fourCharactersWithFirstUpperCase( 'Überlingen' );  // logs false

So we're in good shape for languages that have both uppercase and lowercase versions of the same character. But what about languages that don't have uppercase vs. lowercase characters? Then this code would return false for any string.

I don't have a good solution for that off the top of my head; you'd have to think about how you want to handle that case.

BTW if you really want to try this with a regular expression, there's a possible approach in this answer. Instead of just testing for A-Z, you could list all of the uppercase letters in the languages you may have to deal with. Adapting the regex from that answer, it might look like this:

function beginsWithUpperCase( string ) {
    return /^[A-ZÀÈÌÒÙàèìòùÁÉÍÓÚÝáéíóúýÂÊÎÔÛâêîôûÃÑÕãñõÄËÏÖÜäëïöüçÇßØøÅåÆæÞþÐð]/.test( string );
}

Of course that raises the question of whether we've accurately listed all of the uppercase characters for every language!

本文标签: javascriptHow to check for first letter should be uppercase in JS regexStack Overflow