Case insensitive search in JavaScript

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.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:

  • Before clicking on the button:
  • After clicking on the button:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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 perfom 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>

chevron_right


Output :-

  • Before clicking on button
  • After clicking on button:


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.