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.
replace()method and it will return the rest capital letter in the type of string.
Let’s plan out what we are going to do in this tutorial.
- Find uppercase letters and get them in the string
- Find uppercase letters and get in array
Find uppercase letters and get them in string
1const str = "InfinitBility";23const 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.
Find uppercase letters and get in array
match() method to get the rest string in an array.
1const str = "InfinitBility";23const 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.
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.