How to perform a case-insensitive search in JavaScript?

The search() method searches a string for a specified value, and returns the position of the first occurrence of the specified searchvalue, or -1 if no match is founds.
The search value can be string or a regular expression.

Eg:
Case sensitive search
var str = "WELCOME to WebSpeckle.com";
var n = str.search("COM");
Output is: 3 //Found in third position
var str = "Welcome to WebSpeckle.com";
var n = str.search("COM");
Output is: -1 //Not found
Case insensitive search
var str = "Welcome to WebSpeckle.com";
var n = str.search(/COM/i); Output is: 3 //Found in third position

No comments:

Post a Comment