How to Check if a String Contains Uppercase Letters in JavaScript

Last updated on November 05, 2022
How to Check if a String Contains Uppercase Letters in JavaScript

To check if a string contains uppercase letters in JavaScript, call the test() method on this regular expression /[A-Z]/, i.e., /A-Z/.test(str). test() will return true if the string contains any uppercase letters. Otherwise, it will return false.

For example:

function containsUppercase(str) {
  return /[A-Z]/.test(str);
}

console.log(containsUppercase('javascript')); // false
console.log(containsUppercase('PHP')); // true
console.log(containsUppercase('Coding')); // true

The RegExp test() method searches a string for matches with a specified regular expression and returns true if any are found. Otherwise, it returns false.

The forward slashes (/ /) indicate the start and end of the regular expression.

The square brackets ([ ]) match any one of a given set of characters. The A-Z pattern specifies that these characters be all the letters from A to Z in uppercase. So the complete regular expression matches any capital letter in the string.

Check if string contains only uppercase letters

To check if a string contains only uppercase letters, we'll need to use a slightly different regex: /^[A-Z]+$/.

function containsUppercase(str) {
  return /^[A-Z]+$/.test(str);
}

console.log(containsUppercase('javascript')); // false
console.log(containsUppercase('PHP')); // true
console.log(containsUppercase('Coding')); // false

Let's look into the differences in this new regex:

The ^ character specifies that the pattern must be at the start of the string. Conversely, the $ character indicates that the pattern must be at the end.

The + character matches one or more consecutive occurrences of the preceding pattern in the string.

So matches for the regex will only be found in a string that contains a group of consecutive uppercase letters from the start to the end of the string - a string with only uppercase letters.

Check out this cheat sheet from the MDN docs for a comprehensive guide to regular expression syntax.

String match() method

An alternative to RegExp test() is the String match() method. Instead of calling test() on the regex, we call match() on the string with the regex as an argument.

function containsUppercase(str) {
  return Boolean(str.match(/[A-Z]/));
}

console.log(containsUppercase('javascript')); // false
console.log(containsUppercase('PHP')); // true
console.log(containsUppercase('Coding')); // true

The String match() method returns an array of all the matches of a regular expression in a string. If there are no matches, it returns null.

const str1 = 'javascript';
const str2 = 'Beauty';

console.log(str1?.match(/[A-Z]/)); // null
console.log(str2?.match(/[A-Z]/)); // [ 'B', ... ] (single-item array)

We pass the result of match() to the Boolean() constructor to convert it to a Boolean. Boolean converts truthy values to true, and falsy values to false.

In JavaScript, there are six falsy values: undefinednullNaN0'' (empty string), and false. Every other value is truthy.

console.log(Boolean(undefined)); // false
console.log(Boolean(['letters'])); // true
console.log(Boolean(null)); // false
console.log(Boolean(5)); // true

We used the optional chaining operator (?.) on the string variable. If the variable is undefined or null, this operator will prevent the method call and return undefined instead of throwing an error.

const str = null;

console.log(str?.match(/[A-Z]/)); // undefined

See also