Navigate back to the homepage

How to find capital letters in a string javascript?

Infinitbility
JavaScript
May 23rd, 2022 · 1 min read

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.

1const str = "InfinitBility";
2
3const upperStr = str.replace(/[^A-Z]/g, '');
4console.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.

1const str = "InfinitBility";
2
3const upperStrArr = str.match(/[A-Z]/g, '');
4console.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 👍.

Follow me on Twitter

Join our email list and get notified about new content

No worries, I respect your privacy and I will never abuse your email.

Every week, on Tuesday, you will receive a list of free tutorials I made during the week (I write one every day) and news on other training products I create.

Looking For React Native Tutorial?

we are trying to create the best Tutorial for react native developers.

When you want a daily updates about React Native Tutorial or infinitbility update subscribe to our newsletter.

Read React Native Tutorial

Request New Tutorial or Article on mail [email protected]

Guide book

Guidebook: The Javascript Monorepo Photoshop Guide

Tutorials

Categories

More articles from Infinitbility

How to find lowercase letters in a string javascript?

Example of find lowercase letters in a string in javascript with step by step guide.

May 23rd, 2022 · 1 min read

How to find and remove object from array in javascript?

Example of find and remove object from array in javascript with step by step guide.

May 22nd, 2022 · 1 min read
© 2020–2022 Infinitbility
About
Link to $https://medium.com/infinitbilityLink to $https://www.facebook.com/InfinitbilityLink to $https://github.com/infinitbilityLink to $https://twitter.com/infinitbilityLink to $https://www.buymeacoffee.com/infinitbilityLink to $mailto:[email protected]