Open In App

What is the Role of Ignoring Case RegExp in JavaScript ?

Last Updated : 06 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In JavaScript, we use regular expressions to search and manipulate text. Among those one of the good features of regular expressions is the ability to ignore case sensitivity. Case sensitive means differentiating between capital and lower-case letters. This means that when searching for a pattern, you can specify whether to match uppercase and lowercase characters or not.

The case-insensitive flag (i) is used to achieve this objective. The regular expression is also represented as RegEx in shorthand.

Example:

Javascript




const myString = "I have an Apple";
const myRegEx = /apple/i;
const result = myString.match(myRegEx);
console.log(result);


Output: ["Apple"]

Here, to create a case-insensitive regular expression, we have added the “i” flag after the expression. To search for the word “apple” in a case-insensitive manner, the RegEx pattern would be “/apple/i”.

Approaches: We are going to discuss two different approaches for creating a case-insensitive regular expression pattern in JavaScript.

  • The first approach is to add the “i” flag after the expression, as described above.
  • The second approach is to use character classes, which allows us to match either uppercase or lowercase characters.

For example, the pattern “/[Aa][Pp][Pp][Ll][Ee]/” would match “apple”, “Apple”, “APPLE”, and any other combination of uppercase and lowercase characters.

Example:

Javascript




const myString = "I have an Apple";
const myRegEx = /[Aa][Pp][Pp][Le][Ee]/;
const result = myString.match(myRegEx);
console.log(result);


Output:

["Apple"]

Example 1: We have to validate the e-mail address in the form, and we also want to allow for case-insensitive input. Here we can use a case-insensitive regular expression pattern to match any valid email address, regardless of the case of the letters. The following code describes how we can do it:

Javascript




const emailInput = "user@example.com";
const emailRegEx = /^[^\s@]+@[^\s@]+\.[^\s@]+$/i;
const isValidEmail = emailRegEx.test(emailInput);
console.log(isValidEmail);


Output: 

true

In the example above, our regular expression pattern (/^[^\s@]+@[^\s@]+\.[^\s@]+$/i) checks that the input string contains a sequence of characters before and after the “@” symbol, separated by a dot. The “i” flag is added to the end of the pattern to make it case-insensitive.

Example 2: In this example, we have a string and we are using and we will find whether a particular word exists in the string, regardless of whether it is capitalized or not.

Javascript




let string = "The quick brown fox jumps over the lazy dog";
let pattern = /the/i;
  
if (string.match(pattern)) {
    console.log("Match found!");
} else {
    console.log("No match found.");
}


Output

Match found!

In the example above, the regular expression pattern /the/i matches the word “The” in the string, regardless of its capitalization. The match() method searches the string for the pattern and returns a match object if a match is found.

Example 3: Here is one slightly different application of regular expression:

Javascript




let string = "The quick brown fox jumps over the lazy dog";
let pattern = /fox/i; 
  
let replacedString = string.replace(pattern, "cat");
  
console.log(replacedString);


Output

The quick brown cat jumps over the lazy dog

In the last example, we want to find the word fox in the string variable. If the fox is present we replace that with our new variable cat.

We have a complete list of Javascript RegExp expressions, to check those please go through this JavaScript RegExp Complete Reference article.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads