Find all Uppercase characters in a String using JavaScript


Borislav Hadzhiev

Last updated: Dec 26, 2021


Photo from Unsplash

Find all Uppercase characters in a String #

To find all the uppercase characters in a string, call the replace() method on the string passing it a regular expression, e.g. str.replace(/[^A-Z]/g, ''). The replace method will return a new string containing only the uppercase characters of the original string.

const str = 'HelloWorld'; const onlyUpper1 = str.replace(/[a-z]/g, ''); console.log(onlyUpper1); // 👉️ "HW" const onlyUpper2 = str.replace(/[^A-Z]/g, ''); console.log(onlyUpper2); // 👉️ "HW" const onlyUpper3 = str.match(/[A-Z]/g); console.log(onlyUpper3); // 👉️ ['H', 'W']

We passed the following 2 parameters to the String.replace method:

  1. A regular expression we want to match in the string.
  2. The replacement for each match.

The forward slashes / / mark the beginning and end of the regular expression.

The square brackets [] are called a character class. In the character class we match any letter in the range of a-z, in other words all lowercase latin letters.

We provided an empty string as the replacement for each match, so in essence we replace each lowercase letter with an empty string.

We used the g (global) flag because we want to remove all occurrences of lowercase characters in the string and not just the first occurrence.

The regular expression in the second example is a little different.

const str = '___Hello!!World???'; const onlyUpper2 = str.replace(/[^A-Z]/g, ''); console.log(onlyUpper2); // 👉️ "HW"

In this example, we started the character class with a caret ^. The caret means - "not the following".

In other words, replace all non-uppercase latin letters with an empty string.

This is different from the first example, which only replaced lowercase letters.

The second example replaces any character that isn't an uppercase letter.

The third example returns an array containing the matches.

const str = '___Hello!!World???'; const onlyUpper3 = str.match(/[A-Z]/g); console.log(onlyUpper3); // 👉️ ['H', 'W']

The String.match method returns an array that contains all of the matches of the regex in the string.

Our regular expression matches any uppercase letter and adds each match to the resulting array.

If you need to join the array elements back to a string, you can use the Array.join method.

const str = '___Hello!!World???'; const onlyUpper3 = str.match(/[A-Z]/g)?.join(''); console.log(onlyUpper3); // 👉️ "HW"

In the example, I used the optional chaining (?.) operator because if the match() method doesn't find any matches it returns a null value.

By using the optional chaining (?.) operator we are able to short-circuit instead of get an error for trying to call the join() method on a null value.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.