0

I'm using IndexOf and $.grep in jquery to search a list of values. I need a case insensitive search, but I am getting case sensitivity. I've found similar solutions here on StackOverflow, but hey did not meet my needs. For example none of the solutions found at this link solved my problem: javascript indexOf to ignore Case

Here is the code:

var searchValue = "abc";    
var matches = $.grep(listView.dataSource.view(), function (e) { return e.CategoryName.indexOf(searchValue) >= 0; });
Community
  • 1
  • 1
carlg
  • 1,802
  • 4
  • 25
  • 39
  • 1
    `e.CategoryName.toLowerCase().indexOf(searchValue.toLowerCase())` – Karl-André Gagnon Jan 15 '15 at 15:28
  • Perfect thanks. The difference between your solution and the one that I referenced above (which did not work for me) is the .text() in the other solution. – carlg Jan 15 '15 at 15:33
  • Being able to apply answers that differ by that little is a key ability in programming. I would consider this a duplicate of that other question. – Heretic Monkey Jan 15 '15 at 17:24

1 Answers1

3

Usually in cases where I'm matching the string I'll force it to lower case: str.toLowerCase();

So you want to do that on both sides:

var searchValue = "abc";    
var matches = $.grep(listView.dataSource.view(), function (e) { return e.CategoryName.toLowerCase().indexOf(searchValue.toLowerCase()) >= 0; });

You can use toUpperCase() too but most people use toLowerCase();

Hugo Yates
  • 2,081
  • 2
  • 26
  • 24