Skip to content
Related Articles

Related Articles

Case insensitive search in JavaScript

View Discussion
Improve Article
Save Article
  • Last Updated : 27 Sep, 2021

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. 



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. 


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


  • Before clicking on the button: 

  • After clicking on the button: 

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


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

Output :-

  • Before clicking on button 

  • After clicking on button:


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!