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.
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.
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.)
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...
The string contains a lowercase letter if the expression /[a-z]/ returns true, so simply perform this check, if it's false you have no lowercase letters.
Basic:
^[^a-z]*$
Exclude empty lines:
^[^a-z]+$
Catering for exclusion of non-english chars (e.g. exclude a string containing à like 'VOILà'):
^\P{Ll}*$
Expressions (e.g. JS):
Single line:
/^[^a-z]*$/
Multi line:
/^[^a-z]*$/m
Theory:
[a-z] matches a character that is a,b,c,...z
[a-z]* matches a series of characters that are a,b,c,...z
^[a-z]* matches a series of characters that are not a,b,c...z
^[^a-z]*$ matches a string only containing a series of characters that are not a,b,c,...z from beginning to end.
^[^a-z]+$ ensures that there is at least one character that is not a,b,c,...z, and that the remaining characters are not a,b,c,...z in the string from beginning to end.
^\P{Ll}*$ matches all Unicode characters in the 'letter' group that have an uppercase variant - see https://www.regular-expressions.info/.
If you want to match the string against another regex after making sure that there are no lower case letters, you can use positive lookahead.
^(?=[^a-z]*$)MORE_REGEX$
For example, to make sure that first and last characters are alpha-numeric:
^(?=[^a-z]*$)[A-Z0-9].*[A-Z0-9]$