如何检查JavaScript中的字符串是否全部大写? [英] How can I check if a string is all uppercase in JavaScript?
本文介绍了如何检查JavaScript中的字符串是否全部大写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有一个Javascript / Jquery布尔函数来测试字符串是否全部大写?
There is a Javascript/Jquery boolean function to test if a string is all uppercase?
匹配示例:
"hello" => false
"Hello" => false
"HELLO" => true
推荐答案
function isUpperCase(str) {
return str === str.toUpperCase();
}
isUpperCase("hello"); // false
isUpperCase("Hello"); // false
isUpperCase("HELLO"); // true
您还可以增加 String.prototype
:
String.prototype.isUpperCase = function() {
return this.valueOf().toUpperCase() === this.valueOf();
};
"Hello".isUpperCase(); // false
"HELLO".isUpperCase(); // true
这篇关于如何检查JavaScript中的字符串是否全部大写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文