What is the Role of Ignoring Case RegExp in JavaScript ?
Last Updated :
06 Jun, 2023
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 = "[email protected]";
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.");
}
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);
OutputThe 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.
Similar Reads
JavaScript - What is the Role of Global RegExp?
In JavaScript, global regular expressions (RegExp objects with the g flag) are used to perform operations across the entire input string rather than stopping after the first match. To understand how the g flag works, hereâs a simple example [GFGTABS] JavaScript let s = "apple banana cherry
3 min read
What are the builtin strings in JavaScript ?
A sequence of letters, special characters, numbers, etc., or a combination of all of them is known as a string. Strings are created by enclosing the string characters within a single quote (') or within double quotes ("). Syntax: var myString = 'Good Morning123!'; // Single quoted string var myStrin
3 min read
What is the Meaning of the 'g' Flag in Regular Expressions (JavaScript)?
In JavaScript, the 'g' flag in regular expressions stands for "global." It allows the regular expression to find all matches in a string, rather than stopping after the first match. This flag is particularly useful when you want to work with multiple matches at once. Why Use the 'g' Flag?By default,
2 min read
What is the !! (not not) Operator in JavaScript?
The !! (double negation) operator is a repetition of the unary logical "not" (!) operator twice. It is used to determine the truthiness of a value and convert it to a boolean (either true or false). Hereâs how it works: The single ! (logical "not") inverts the truth value of a given expression:!fals
2 min read
What are the Gotchas in JavaScript ?
Javascript truly is a popular language due to its simplicity and versatility. Despite having many merits, Javascript is a funny language that can confuse you at times especially for those accustomed to the traditional OOP language. The tricky parts or 'gotchas' (not limited to the following) are: ==
3 min read
JavaScript - How to Use RegExp in Switch Case Statements?
To make your JavaScript switch statements more dynamic and flexible, you can use regular expressions (RegExp). Here are the various ways to use RegExp in switch case statement. 1. Using test() Method in switchThe test() method can be used to check if a string matches a given regular expression. Letâ
3 min read
JavaScript - What is RegExp Object?
The RegExp object in JavaScript is a powerful tool for pattern matching, searching, and manipulating strings. It allows you to define patterns for matching text and helps in validation, extraction, and replacement. 1. String SearchingCheck if a String Contains a Word [GFGTABS] JavaScript let s =
2 min read
Find Word Character in a String with JavaScript RegExp
Here are the different ways to find word characters in a string with JavaScript RegExp 1. Using \w to Find Word CharactersThe \w metacharacter in regular expressions matches any word character, which includes: A to Z (uppercase letters)a to z (lowercase letters)0 to 9 (digits)Underscore (_)You can u
3 min read
What does OR Operator || in a Statement in JavaScript ?
JavaScript is a dynamic programming language that allows developers to write complex code with ease. One of the fundamental concepts in JavaScript is the use of operators, which are symbols that perform operations on one or more values. One such operator is the || (logical OR) operator, which can be
6 min read
What is the difference between find() and filter() methods in JavaScript ?
In this article, we will see the difference between the find() and filter() methods in javascript. JavaScript find() method: The find() method is used to find all the descendant elements of the selected element. It finds the element in the DOM tree by traversing through the root to the leaf. Syntax:
2 min read