如何使用JavaScript测试字符串中的字母是大写还是小写?
如果字符或整个字符串为数字,则josh和maleki的答案在大写和小写形式上都将返回true。使结果为假结果。使用乔什的例子
var character = '5'; if (character == character.toUpperCase()) { alert ('upper case true'); } if (character == character.toLowerCase()){ alert ('lower case true'); }
另一种方法是先测试它是否为数字,否则测试它是否为大写或小写示例
var strings = 'this iS a TeSt 523 Now!'; var i=0; var character=''; while (i <= strings.length){ character = strings.charAt(i); if (!isNaN(character * 1)){ alert('character is numeric'); }else{ if (character == character.toUpperCase()) { alert ('upper case true'); } if (character == character.toLowerCase()){ alert ('lower case true'); } } i++; }