How to find uppercase words in array JavaScript

In this, we will learn How to find uppercase words in array Javascript or How to find uppercase words in an array in Javascript using Regex. 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.

1. How to find uppercase words in array Javascript


In this example, We are iterating over the array using the map() function on each iteration to match the array element against a regular expression using the Javascript match() function and return an array of uppercase words if the array element is uppercase. When the array element is not uppercase return an array of null elements and append it into a new array resArr.

In the second step we are filtering null from array using filter(x => x !== null).flat()) and flattening an array of arrays using the flat() method.

  • / /: 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 myArr = ['APP','DATA','SO','Java','do']
var resArr=[];

myArr.forEach((element) => {
  
CaptialWords = element.match(/(\b[A-Z][A-Z]+|\b[A-Z]\b)/g);
 resArr.push(CaptialWords)  
})

console.log(values.filter(x => x !== null).flat())

Output

[ 'APP', 'DATA', 'SO' ]

2. How to find uppercase alphanumeric words in array 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 myArr = ['AP369P','D5656ATA','SO','Java','do']
var values =[];

myArr.forEach((element) => 
{
  
CaptialWords = element.match(/(\b[A-Z0-9][A-Z0-9]+|\b[A-Z]\b)/g);
 values.push(CaptialWords)  
})

console.log(values.filter(x => x !== null).flat())

Output

[ 'AP369P', 'D5656ATA', 'SO' ]

Summary

In this post, we have learned how to find the uppercase words in array Javascript using Regex. We have also covered How to find uppercase word in array Javascript and How to find alphanumeric uppercase word in array Javascript.