How can I check a word is already all uppercase?

后端 未结 4 2172
滥情空心
滥情空心 2020-12-06 09:16

I want to be able to check if a word is already all uppercase. And it might also include numbers.

Example:

GO234 => yes
Go234 => no
         


        
4条回答
  •  广开言路
    2020-12-06 09:58

    a = "Go234"
    a.match(/\p{Lower}/) # => #
    
    b = "GO234"
    b.match(/\p{Lower}/) # => nil
    
    c = "123"
    c.match(/\p{Lower}/) # => nil
    
    d = "µ"
    d.match(/\p{Lower}/) # => #
    

    So when the match result is nil, it is in uppercase already, else something is in lowercase.

    Thank you @mu is too short mentioned that we should use /\p{Lower}/ instead to match non-English lower case letters.

提交回复
热议问题