JavaScript RegExp – [A-Z]

JavaScript RegExp – [A-Z]

RegExp, short for Regular Expression, is a pattern that describes a set of strings. Regular expressions are used to perform versatile operations on strings, such as searching, replacing, and validating. In JavaScript, we can create regular expression patterns using the RegExp object.

One of the most commonly used regular expression patterns is the range of characters selector, represented by square brackets. The [A-Z] pattern matches any uppercase alphabetic character from A to Z. Let’s take a closer look at how this works with some sample code.

let str = "Hello World";
let pattern = /[A-Z]/g;

let results = str.match(pattern);
console.log(results); // Output: ["H", "W"]

In the code above, we declare a string variable called str with the value “Hello World”. We then define a regular expression pattern using the / delimiters and square brackets [A-Z], which match uppercase alphabetic characters. The g flag at the end of the pattern means to match globally in the input string.

We then use the match() method of the string object to execute the regular expression pattern against our str variable. The match() method returns an array of all the matches it found in the string. In this case, we get an array with the values "H" and "W", which are the uppercase letters in our input string.

We can also use the [A-Z] pattern in other regex operations. For example, we can replace all uppercase letters in a string with a specific character, such as a hyphen, using the replace() method.

let str = "Hello World";
let pattern = /[A-Z]/g;

let replaced = str.replace(pattern, "-");
console.log(replaced); // Output: "-ello -orld"

In this example, we still define our regular expression pattern using the [A-Z] range of characters selector. We then use the replace() method of the string object to replace all uppercase characters in the original string with a hyphen “-“. The output of this code is the modified string, where uppercase letters are replaced with hyphens.

Another common operation we can perform using [A-Z] is string validation. We can check if an input string matches a specific pattern using the test() method.

let pattern = /^[A-Z][a-z]+$/;
let isValid = pattern.test("ValidString");
console.log(isValid); // Output: true

isValid = pattern.test("Invalid-string");
console.log(isValid); // Output: false

In this example, we define a regular expression pattern /^[A-Z][a-z]+$/, which matches strings that start with an uppercase letter, followed by one or more lowercase letters. The ^ and $ symbols mark the beginning and end of the string, respectively.

We then use the test() method of the RegEx object to check if the input string "ValidString" matches the pattern. Since "ValidString" starts with an uppercase letter and is followed by only lowercase letters, the output of this code is true. Similarly, when we test "Invalid-string", the output is false, since it starts with a lowercase letter.

In conclusion, the [A-Z] range of characters selector is a powerful tool for working with regular expressions in JavaScript. It allows us to match uppercase alphabetic characters in strings, perform replacements, and validate input according to a specific pattern. Understanding how to use regular expressions is an essential skill for any JavaScript developer, and the [A-Z] pattern is a great place to start.

Like(0)