Hi Friends 👋,

Welcome To Infinitbility! ❤️

Today, we are going to do how I find capital ( uppercase ) letters in a string, here we will use some common regex to find all capital letters from a string.

Here, we will learn to check whether the uppercase letter contains in string or not also, and if contain it will return as a string and array format.

  • To remove a lowercase letter from a string we will use the javascript replace() method and it will return the rest capital letter in the type of string.

  • To find an uppercase letter in a string and return it in the form array we will use the javascript match() method.

Let’s plan out what we are going to do in this tutorial.

  1. Find uppercase letters and get them in the string
  2. Find uppercase letters and get in array

Let’s start today’s tutorial How to find capital letters in a string javascript?

Find uppercase letters and get them in string

In this example, To find uppercase letters we will use regex and javascript replace() method.

const str = "InfinitBility";

const upperStr = str.replace(/[^A-Z]/g, '');
console.log(upperStr); // 👉️ 'IB'

In the above example, we replace lowercase letters with empty and returning rest string.

The Output should show ‘IB’ only. let’s check it.

javascript, find capital letters in a string example
javascript, find capital letters in a string example

Find uppercase letters and get in array

In this example, To find uppercase letters we will use the regex and javascript match() method to get the rest string in an array.

const str = "InfinitBility";

const upperStrArr = str.match(/[A-Z]/g, '');
console.log(upperStrArr); // 👉️ [ 'I', 'B' ]

In the above example, we find capital letters in string and returning char in the array.

The output should show [ 'I', 'B' ] only. let’s check it.

javascript, find capital letters in a string example
javascript, find capital letters in a string example

I hope it’s help you, All the best 👍.