Try
$(function() {
$.getJSON('./banco/typeahead.json'
, function (data) {
var substringMatcher = function (strs) {
return function findMatches(q, cb) {
var matches, substrRegex;
// an array that will be populated with substring matches
matches = [];
// added `á`:`a` to `charMap` ,
// e.g., try "à" === "á" : false
var charMap = {
"á": "a",
"à": "a",
"â": "a",
"á": "a",
"é": "e",
"è": "e",
"ê": "e",
"ë": "e",
"ï": "i",
"î": "i",
"ô": "o",
"ö": "o",
"û": "u",
"ù": "u"
};
// generate string "map" of `data`
var _map = $.map(data, function (v, k) {
return [v]
}).join(",").replace(/,/g, "");
var temp = [];
var count = null;
var regex, r, _temp;
var normalize = function (str, map) {
var _temp = new RegExp(str, "i");
$.each(charMap, function (chars, normalized) {
// var regex = new RegExp('[' + chars + ']', 'gi');
// str = str.replace(regex, normalized);
if (_temp.test(normalized)) {
++count;
temp.push(chars);
var _str = temp.join("|");
if (count === temp.length) {
_str = new RegExp(_str, "gi");
regex = _str;
};
};
});
// test for `str` in `data`
// if present, return `str` as regex,
// else , return `str` mapped to `charMap`
var r = _temp.test(map) ? _temp : regex;
return r;
};
// regex used to determine if a string
// contains the substring `q`
// pass `data` (typeahead.json) to
// `normalize` to test for occurences
// of `str` in `data`
substrRegex = normalize(q, _map);
// iterate through the pool of strings
// and for any string that
// contains the substring `q`,
// add it to the `matches` array
$.each(strs, function (i, str) {
if (substrRegex.test(str)) {
// the typeahead jQuery plugin expects
// suggestions to a
// JavaScript object, refer to typeahead
// docs for more info
matches.push({
value: str
});
}
});
cb(matches);
};
};
var banco = data;
$('.typeahead').typeahead({
hint: true,
highlight: true,
minLength: 1
}, {
name: 'banco',
displayKey: 'value',
source: substringMatcher(banco)
});
});
});
jsfiddlehttp://jsfiddle.net/guest271314/urqcq20d/ http://jsfiddle.net/guest271314/urqcq20d/