How can you determine if a string is all caps with a regular expression. It can include punctuation and numbers, just no lower case letters.
To check if a letter in a string is uppercase or lowercase use the toUpperCase() method to convert the letter to uppercase and compare it to itself. If the comparison returns true , then the letter is uppercase, otherwise it's lowercase. Copied!
For example, the regular expression "[ A-Za-z] " specifies to match any single uppercase or lowercase letter. In the character set, a hyphen indicates a range of characters, for example [A-Z] will match any one capital letter.
[] denotes a character class. () denotes a capturing group. [a-z0-9] -- One character that is in the range of a-z OR 0-9.
m/^[^a-z]*$/
For non-English characters,
m/^\P{Ll}*$/
(\P{Ll}
is the same as [^\p{Ll}]
, which accepts all characters except the ones marked as lower-case.)
That sounds like you want: ^[^a-z]*$
Why not just use if(string.toUpperCase() == string)? ._. Its more "elegant"...
I think you're trying to force in RegExp, but as someone else stated, I don't think this is the best use of regexp...
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