jQuery Data Type How to - Determine the case (upper/lower) of the first letter in a string








Question

We would like to know how to determine the case (upper/lower) of the first letter in a string.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'
  src='http://code.jquery.com/jquery-1.6.4.js'></script>
<script type='text/javascript'>
<!--from  w  w  w.  j  a v a 2s .co m-->
function getFirstCase(s) {
    return (/^[\d\W]*[A-Z]/).test(s) ? 'upper' :
        (/^[\d\W]*[a-z]/).test(s) ? 'lower' :
        'none';
}
document.writeln(getFirstCase('alphabet'));
document.writeln(getFirstCase('AAA'));
</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: