JavaScript: Extend String Object: Attach toUpperCase() Method to String.prototype

Using a String.prototype Method to Check for All UpperCase Letters

nick3499
1 min readJun 19, 2018
Captured from Node.js REPL

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 words
  • g indicates every matching character
> s = 'CAPITALIZED'
'CAPITALIZED'
> s.match(/[A-Z\s]/g)
[ 'C', 'A', 'P', 'I', 'T', 'A', 'L', 'I', 'Z', 'E', 'D' ]
>

--

--

nick3499
nick3499

Written by nick3499

coder of JavaScript and Python

No responses yet