I have searched it for a while. But there are no perfect answers.
For example, someone says I can use:
function isUpperCase(str) {
return str === str.toUpperCase();
}
This works for a simple case, but not for complicated ones.
For instance:
isUpperCase('ABC'); // works good
isUpperCase('ABcd'); // works good too
isUpperCase('汉字'); // not working, should be false.
How about
function isUpperCase(str) {
return str === str.toUpperCase() && str !== str.toLowerCase();
}
to match the last one
RegExp
/^[A-Z]+$/
returns expected result
const isUpperCase = str => /^[A-Z]+$/.test(str);
console.log(
isUpperCase("ABC")
, isUpperCase("ABcd")
, isUpperCase("汉字")
);
You can try regex approach.
const isUpperCase2 = (string) => /^[A-Z]*$/.test(string);
isUpperCase2('ABC'); // true
isUpperCase2('ABcd'); // false
isUpperCase2('汉字'); // false
Hope this help;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With