JavaScript: Extend String Object: Attach toUpperCase() Method to String.prototype
Using a String.prototype Method to Check for All UpperCase Letters
Lengths for Comparison
match()
along with a regex pattern parameter, followed by length()
, were used to count the uppercase letters (including spaces) below. Total length of the entire string were also returned:
> s = 'CAPITALIZED CHARACTERS'
'CAPITALIZED CHARACTERS'
> s.match(/[A-Z\s]/g).length
22
> s.length
22
>
Regex Pattern
The regex pattern /[A-Z\s]/g
has three basic aspects:
[A-Z]
matches uppercase letters\s
matches spaces between wordsg
indicates every matching character
> s = 'CAPITALIZED'
'CAPITALIZED'
> s.match(/[A-Z\s]/g)
[ 'C', 'A', 'P', 'I', 'T', 'A', 'L', 'I', 'Z', 'E', 'D' ]
>