Open In App

Case insensitive search in JavaScript

Last Updated : 27 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Case-insensitive: It means the text or typed input that is not sensitive to capitalization of letters, like “Geeks” and “GEEKS” must be treated as same in case-insensitive search. In Javascript, we use string.match() function to search a regexp in a string and match() function returns the matches, as an Array object. 

Syntax:  

string.match(regexp)

Parameters: This method accepts single parameter regexp which is required. It is used to pass the value to search for as a regular expression.

Regular expression (regexp): It is a particular syntax /pattern/modifiers; modifier sets the type. For example /GeeksforGeeks/i where “i” sets to case insensitive. 

Note: Here g and i used for global and case-insensitive search respectively.

Example 1: This example describes the search of a regular expression. 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>
        Case insensitive search
        in JavaScript
    </title>
</head>
 
<body style = "text-align:center;">
 
    <h1 style = "color:green;" >    
        GeeksforGeeks
    </h1>
     
     
<p>
        Click on button
    </p>
 
 
    <button onclick="myGeeks()">
        Click Here!
    </button>
 
    <p id="GFG"></p>
 
 
    <script>
        function myGeeks() {
            var str = "Welcome Geeks GEEKS geeks";
            var res = str.match(/geeks/gi);
             
            document.getElementById("GFG").innerHTML
                    = res;
        }
    </script>
</body>
 
</html>                   


Output: 

  • Before clicking on the button: 

  • After clicking on the button: 

Example 2: This example describes the search of a regular expression. 

HTML




<!DOCTYPE html>
<html>
 
<head>
    <style>
        h1 {
            padding-top: 35px;
            color: green;
        }
    </style>
</head>
 
<body style="text-align:center;">
 
    <h1>GeeksforGeeks</h1>
 
     
<p>Click the button to perform a global
                     case-insensitive search.</p>
 
 
    <button onclick="myFunction()">Try it</button>
 
    <p id="demo"></p>
 
 
    <script>
        function myFunction() {
            var str = "Brazil and Austraila won the WorldCup 5 times";
            var res = str.match(/5/gi);
            document.getElementById("demo").innerHTML = res;
        }
    </script>
 
</body>
 
</html>


Output :-

  • Before clicking on button 

  • After clicking on button:

 



Similar Reads

Compare the Case Insensitive strings in JavaScript
Comparing strings in a case-insensitive manner means comparing them without taking care of the uppercase and lowercase letters. Here are some common approaches to compare the case-insensitive string in JavaScript: Table of Content JavaScript toUpperCase() functionJavaScript toLowerCase() functionUsing localCompare() Using regular expressionApproach
2 min read
How to make Array.indexOf() case insensitive in JavaScript ?
The task is to make the Array.indexOf() method case insensitive. Here are a few of the techniques discussed with the help of JavaScript. Table of Content Using JavaScript toLowerCase() Method Using JavaScript toUpperCase() Method: Using Array.findIndex() and RegExpUsing JavaScript toLowerCase() Method Transform the search string and elements of the
2 min read
How to Convert Camel Case String to Snake Case in JavaScript ?
We are going to learn about the conversion of camel case string into snake case by using JavaScript. Camel case string means words combined, each starting with uppercase and Snake case means words joined with underscores, all lowercase, Converting camel case to snake case involves transforming strings from a format where words are separated by unde
3 min read
How to convert camel case to snake case in JSON response implicitly using Node.js ?
Camel case and snake case are two common conventions for naming variables and properties in programming languages. In camel case, compound words are written with the first word in lowercase and the subsequent words capitalized, with no spaces or underscore between them. For example, firstName is written in camel case. In snake case, compound words
5 min read
How to Convert Camel Case to Snake Case in PHP ?
Given a Camel Case String, the task is to convert the Camel Case String to a Snake Case String in PHP. Examples: Input: GeeksForGeeksOutput: geeks_for_geeksInput: WelcomeToGfg Output: welcome_to_gfgCamel case uses capital letters at the beginning of each word except the first one, while snake case separates words with underscores and uses all lower
1 min read
How to add default search text to search box in HTML with JavaScript ?
We can add a default search text to the search box by using the JavaScript DOM getElementById() method and the onfocus &amp; onblur events. getElementById(): The DOM getElementById() method is used to return values to the unique id of the input tag used in HTML. It is one of the most widely used DOM HTML methods when we want to manipulate and get i
3 min read
How to convert hyphens to camel case in JavaScript ?
Given a string containing hyphens (-) and the task is to convert hyphens (-) into camel case of a string using JavaScript. Approach: Store the string containing hyphens into a variable.Then use the RegExp to replace the hyphens and make the first letter of words upperCase. Example 1: This example converts the hyphens ('-') into camel case by using
2 min read
Access Object property case-insensitively in JavaScript
In this article, we will see how to access an object's properties without case-sensitivity, In this article, we are discussing how to access object property case-insensitively in JavaScript. Using toLowerCase() and Object.hasOwnProperty() MethodsUsing Object.keys() and toLowerCase() MethodsMethod 1: Using toLowerCase() and Object.hasOwnProperty() M
2 min read
What is a typical use case for anonymous functions in JavaScript ?
In this article, we will try to understand what exactly an Anonymous function is, and how we could declare it using the syntax provided in JavaScript further we will see some examples (use-cases) where we can use anonymous functions to get our results in the console. Before proceeding with the examples or use cases of anonymous functions, let's bri
4 min read
What is the Role of Ignoring Case RegExp in JavaScript ?
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 lowe
3 min read
Article Tags :