Find Uppercase words in string JavaScript using Regex

In the post, we will learn how to Find Uppercase words in string JavaScript using Regex with examples. The JavaScript string. match() function is used to filter strings using regular expression and returns an array after matching a string against a regular expression. The regular expressions are patterns used to map a character combination in a string. The regular expression is created in two ways in JavaScript.

  • Using Regular Expression Literal: in which pattern is enclosed between slashes example.
const reg = /cdc+c/;
  • Using RegExp Constructor
const re = new RegExp('ab+c');

1. Find Uppercase words in string JavaScript using Regex


In this example, we have created a regular expression using literal syntax to Find Uppercase words in string JavaScript using Regex. The Regular expression is made using the following pattern. First, we will understand how the regular expression is working step by step.

  • / /: The Regular expression enclosed in between slashes
  • \b: Start of the word
  • [A-Z]: Upper case Letter between A to Z.
  • [A-Z]+: Upper case letter Between A To Z repeated one or more times
  • |: The OR operator
  • \b: start of the word
  • [A-Z]: Upper Case letter Between A to Z
  • \b: End of word
  • /g : The “g” flag indicates that the regular expression should be tested against all possible matches in a string

const myStr = "Welcome folks'S TO DEVENUM! HOW ARE YOU";
const CaptialWords = myStr.match(/(\b[A-Z][A-Z]+|\b[A-Z]\b)/g);

console.log(CaptialWords);
console.log(CaptialWords.toString();)

Output

[ 'S', 'TO', 'DEVENUM', 'HOW', 'ARE', 'YOU' ]

2. Find Uppercase words in string JavaScript with quotation


In this example, We will understand how to Find Uppercase words in string JavaScript using Regex with quotations we have changed the Regular Expression by adding the [‘A-Z]+ to include the upper case letter that contains quotation marks. Let us understand with the below code and output, how it is working.

const myStr = "WELCOME'S folks TO DEVENUM! HOW ARE 'Y'OU";
const CaptialWords = myStr.match(/(\b[A-Z]['A-Z]+|\b[A-Z]\b)/g)

console.log(CaptialWords);
console.log(CaptialWords.toString())

Output

[ "WELCOME'S", 'TO', 'DEVENUM', 'HOW', 'ARE', "Y'OU" ]
WELCOME'S,TO,DEVENUM,HOW,ARE,Y'OU

3. How to get Uppercase alphanumeric words in string JavaScript


In this example, we will understand how to get the uppercase alphanumeric word in a string by using regular expressions. To check for the alphanumeric string we have changed the regular expression A-Z0-9][A-Z0-9] which includes uppercase words along with the digit. The 0-9 means the digit between 0-9.

const myStr = "Welcome D2'S TO D56EV ENUM! H69OW A89E Y20OU";
const CaptialWords = myStr.match(/(\b[A-Z0-9][A-Z0-9]+|\b[A-Z]\b)/g)

console.log(CaptialWords);
console.log(CaptialWords.toString())

Output

[
  'D2',   'S',
  'TO',   'D56EV',
  'ENUM', 'H69OW',
  'A89E', 'Y20OU'
]
D2,S,TO,D56EV,ENUM,H69OW,A89E,Y20OU

4. How to get Uppercase alphanumeric words in string with quotation


In this example, we have to find the uppercase word in a string that contain letters and digit along with quotation mark. Let us understand with the below example and output how it is working.

const myStr = "Welcome D2'S TO D56EV ENUM! H69OW A89E Y20OU";
const CaptialWords = myStr.match(/(\b[A-Z0-9]['A-Z0-9]+|\b[A-Z]\b)/g);


console.log(CaptialWords);
console.log(CaptialWords.toString())

Output

[
  "D2'S",  'TO',
  'D56EV', 'ENUM',
  'H69OW', 'A89E',
  'Y20OU'
]
D2'S,TO,D56EV,ENUM,H69OW,A89E,Y20OU

Summary

In this post, we have learned how to Find Uppercase words in string JavaScript using Regex with examples by using Javascript built-in function string. match() that matches a string against a regular expression and returns an array.