For example, the following code searches for a specific word in a string, regardless of the case:
The `i` flag in the regular expression specifies a case-insensitive search. Without it, the search would be case-sensitive, and the expression would return `null`.
const uppercaseLetters = str.match(/[A-Z]/g);
console.log(uppercaseLetters); // logs [“T”, “P”, “R”, “E”, “M”, “U”, “L”, “J”]
In this example, the match() method is used to search for all uppercase letters in the string and return them as an array.
/[A-Z]/g pattern. Here, the square brackets contain a range of characters to match, in this case, all uppercase characters from A to Z. The “g” flag at the end ensures that the search is global, meaning that it will return all matches in the string.
For example, consider the following code:
const str = "Hello World"; const uppercaseRegex = /[A-Z]/g; const matches = str.match(uppercaseRegex); console.log(matches); // Output: ["H", "W"]
In this example, the code searches for all uppercase characters in the string “Hello World” and stores the matches in an array using the
match() method. The console output returns an array containing the uppercase letters “H” and “W”.
Matching Single Uppercase Characters
To match a single uppercase character, you can use the
[A-Z] character class. This will match any uppercase letter from A to Z:
const regex = /[A-Z]/; console.log(regex.test('A')); // true console.log(regex.test('b')); // false
If you want to match only one specific uppercase character, you can simply specify it within the character class:
const regex = /[D]/; console.log(regex.test('D')); // true console.log(regex.test('E')); // false
Matching Multiple Uppercase Characters
If you want to match strings that contain multiple uppercase characters, you can use the
+ quantifier. This will match one or more of the character that precedes it:
const regex = /[A-Z]+/; console.log(regex.test('HELLO')); // true console.log(regex.test('hello')); // false
If you want to match strings that contain a specific sequence of uppercase characters, you can use a capturing group to specify the exact pattern you want to match:
const regex = /(ABC)+/; console.log(regex.test('ABCABC')); // true console.log(regex.test('ABCD')); // false
Matching Uppercase Words
If you want to match strings that contain entire words in uppercase, you can use the
\b word boundary anchor. This will match the beginning or end of a word:
const regex = /\b[A-Z]+\b/; console.log(regex.test('HELLO WORLD')); // true console.log(regex.test('Hello World')); // false
- Use the
\bboundary to match uppercase letters at the beginning of a word. For example,
/\b[A-Z]/will match any uppercase letter at the beginning of a word.
- Use the
\Bboundary to match uppercase letters within a word. For example,
/\B[A-Z]/will match any uppercase letter not at the beginning of a word.
- Use the
/gflag to match all occurrences of an uppercase letter in a string. For example,
/[A-Z]/gwill match all uppercase letters in a string.
- Combine multiple patterns with the
|operator. For example,
/\b[A-Z]|\B[A-Z]/gwill match all uppercase letters at the beginning of a word and within a word.
- Remember that regular expressions are case-sensitive by default. Use the
iflag to make the matching case-insensitive. For example,
/[A-Z]/giwill match all uppercase letters regardless of case.
- Validating that a string starts with an uppercase letter:
- Replacing all occurrences of uppercase letters in a string:
- Matching strings that contain only uppercase letters:
let uppercaseRegex = /^[A-Z]/; //matches the first uppercase letter
let str = “Hello World”;
let replacedStr = str.replace(/[A-Z]/g, “X”); //returns “XXXXX XXXXX”
let uppercaseOnlyRegex = /^[A-Z]+$/; //matches if string contains only uppercase letters
One of the most common issues is failing to use the case-sensitive flag when matching uppercase letters. By default, most regex engines are case-insensitive, which means that they will match both uppercase and lowercase letters without distinction. To match only uppercase letters, you’ll need to include the “g” flag in your regex pattern, which stands for “global” and tells the engine to match all occurrences of the pattern.