JavaScript > Regular Expressions Modifiers

Case insensitive search in JavaScript

How to do a case-insensitive search for a specified character in a string in JavaScript?


Case-insensitive search

Case-insensitive is an search in JavaScript, which can be done by using regular expression i modifier.

Regular expression is nothing but an object which describes a pttern of characters.

In JavaScript, by using JavaScript Regular Expression i modifeir we can do a case-insensitive search for a specified character in a string.

NOTE: We can use ignoreCase property for finding whether "i" modifier is used or not.

Case-insensitive search in a string.

<p>Click the below button to do case sensitive seacrh.</p>
<input type="button" value="Click" onclick="mySearch()" />
<p id="myId"></p>

<script>
    function mySearch() {
        var a = "TechFunda is a sister Concern of DotNetFunda";
        var b = /concern/i;
        var r = a.match(b);
        document.getElementById("myId").innerHTML = r;
    }
</script>

In the above code snippet we have given Id as "myId"to the second <p> element in the HTML code. There is a function mySearch() in the script code which is connected to the Onclick of the HTML button.

A string with value "TechFunda is a sister Concern of DotNetFunda" is given to the variable a, from the string we need to do a case-insensitive search for a character 'concern' in a string, for that we are using i modifier in the variable b, variable rreturns the case-insensitive search result.

Onclick of the button "Click" in the HTML page fires the function mySearch() in the script code, at the same time i modifier do the case-insenstive search for a character 'concern' in a string and returns the output.

OUTPUT

 Views: 13069 | Post Order: 138



Write for us






Hosting Recommendations