I'm using the jQuery UI Autocomplete with a local datasource (source: myArray
). I want the autocomplete to propose only the results that start with the string entered instead of the default case-insensitive contains search. Is there a simple solution for this or do I have to provide my custom search/source callback?

- 9,925
- 7
- 47
- 64
-
2Related answer: http://stackoverflow.com/questions/2382497/jquery-autocomplete-plug-in-search-configuration – sina Aug 15 '11 at 00:57
6 Answers
See this:
Match start word:
http://blog.miroslavpopovic.com/jqueryui-autocomplete-filter-words-starting-with-term
He overrides the autocomplete filter method. I use this and it works well.
// Overrides the default autocomplete filter function to search only from the beginning of the string
$.ui.autocomplete.filter = function (array, term) {
var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(term), "i");
return $.grep(array, function (value) {
return matcher.test(value.label || value.value || value);
});
};
Match word:
Match startsWith of any word in the string.
e.g. "LHR london" is matched by "london"
var matcher = new RegExp("\\b" + $.ui.autocomplete.escapeRegex(term), "i");
\b assert position at a word boundary (^\w|\w$|\W\w|\w\W)

- 3,680
- 4
- 26
- 52

- 4,689
- 2
- 34
- 43
-
How would you switch it back to the default filter? I'm asking because: `if(userInputsNumber) { useDefault; } else { useStartsWith(); }` – Kellen Stuart Mar 22 '19 at 16:40
-
var matcher = new RegExp("\\b" + $.ui.autocomplete.escapeRegex(term), "i"); works great for numbers and strings. – Muhammad Haroon Apr 23 '19 at 17:41
Currently I've done it this way, not sure if there is a better solution:
source: function(request, response) {
var filteredArray = $.map(orignalArray, function(item) {
if( item.value.startsWith(request.term)){
return item;
}
else{
return null;
}
});
response(filteredArray);
},
This approach also made it possible to impose a limit (e.g. 10 items) on the amount of items to be shown.

- 9,925
- 7
- 47
- 64
-
I am by no means an expert on these things but can imagine a string matching algorithm like Aho–Corasick can help. – Huppie Jun 30 '10 at 13:10
-
Before you misunderstand my previous comment. Implementing string matching yourselves should only be done when performance is absolutely critical AND a profiler shows you your current solution is the bottleneck. Until then, use the solution you have now. Readability of your code trumps the performance benefits in this case :) – Huppie Jun 30 '10 at 15:35
-
Your proposal about the string matching algorithm also means a custom callback method like mine above (if I'm not wrong). It might be interesting in some particular cases like you say, even though the UI Autocomplete already implements a decent search (with contains). For now I'll stick with my implementation (I only got 3k items of 4chars each). – Bart Jul 01 '10 at 21:26
-
-
I've put in the same code to make it work by 'start with', but it does not work, now no dropdown appears at all as I inserted it :( What might be my problem? May be some additional scripts should be included – YMC Mar 16 '12 at 20:11
-
UPDATE to my previous comment: I've found it's startWith function that broken javascript execution. Looks like there is no such a function in JavaScript. I use indexOf instead like this if `(item.indexOf(request.term) == 0) { ...` – YMC Mar 16 '12 at 21:24
-
Might be that I wrote a startsWith function, don't have the code with me right now. http://rickyrosario.com/blog/javascript-startswith-and-endswith-implementation-for-strings/ – Bart Mar 17 '12 at 09:56
you can use Same way into Jquery UI Autocomplete Examples
<script>
$("#autocompleteInputField").autocomplete({
source: function(request, response) {
var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term), "i");
response($.grep(myArray, function(item){
return matcher.test(item);
}) );
}
});
</script>
OR another way with using $.map
method not $.grep
<script>
$("#autocompleteInputField").autocomplete({
source: function(request, response) {
var matcher = new RegExp("^" + $.ui.autocomplete.escapeRegex(request.term), "i");
response($.map(myArray, function(item) {
if (matcher.test(item)) {
return (item)
}
}));
}
});
</script>

- 5,096
- 1
- 47
- 58
Here's a slightly different way to do case sensitive searching. Note the lack of "i" in the creation of the regexp in the second example, which is what causes case insensitivity in the default implementation.
case insensitive:
$('#elem').autocomplete({
source: array
});
case sensitive:
$('#elem').autocomplete({
source: function(request, response) {
var matcher = new RegExp( $.ui.autocomplete.escapeRegex(request.term, ""));
var data = $.grep( array, function(value) {
return matcher.test( value.label || value.value || value );
});
response(data);
}
});

- 111
- 1
- 2
I went into the Jqueryui code and switched it there.
If you look in the auto complete section, you will see the following line:
filter:function(a,b){var g=new RegExp(d.ui.autocomplete.escapeRegex(b),"i")
Modify it to the following (beware, this is a global change):
filter:function(a,b){var g=new RegExp("^" + d.ui.autocomplete.escapeRegex(b),"i")

- 21
- 1
source: function( request, response ) {
var t = jQuery.grep(t, function(a){
var patt = new RegExp("^" + request.term, "i");
return (a.match(patt));
});
response(t);
},

- 1,172
- 9
- 22
-
-
1I've noticed that now also. Or it needs to be a.match(patt). I don't know how/why it worked before, but it did. – Jamie R Rytlewski Aug 11 '11 at 16:35
-
What if i wanted to get words that endsWith??? i tried this regular expression, var patt = new RegExp("\\>" + request.term, "i"); but it didn't worked, btw, what does the second parameter for the function RegExp named "i" mean ?? – Tarek Aug 24 '13 at 13:19