- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我试过了 this solution但我得到了这个错误:
Uncaught ReferenceError: normalized is not defined
这是我的代码:
var charMap = {
"à": "a", "â": "a", "é": "e", "è": "e", "ê": "e", "ë": "e",
"ï": "i", "î": "i", "ô": "o", "ö": "o", "û": "u", "ù": "u"
};
var normalize = function(str) {
$.each(charMap, function(chars, normalized) {
var regex = new RegExp('[' + chars + ']', 'gi');
str = str.replace(regex, normalized);
});
return normalized;
}
var queryTokenizer = function(q) {
var normalized = normalize(q);
return Bloodhound.tokenizers.whitespace(normalized);
};
var spectacles = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: queryTokenizer,
prefetch:'spectacles.json',
limit:10,
});
spectacles.initialize();
$('#search').typeahead({
minLength: 1,
hint:false,
highlight: true
},
{
name: 'spectacles',
displayKey: 'value',
source: spectacles.ttAdapter()
}) ;
我的错误在哪里?谢谢
最佳答案
如果您不想使用 Bloodhound,您可以从 Typeahead 对象自定义“highlighter”和“matcher”方法,使它们变得不区分重音。
如果你想让 typeahead 的默认行为不区分重音,你可以包含一个新的 JavaScript 文件,如下所示:
// function for making a string accent insensitive
$.fn.typeahead.Constructor.prototype.normalize = function (str) {
// escape chars
var normalized = str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
// map equivalent chars
normalized = normalized.replace(/[aãáàâ]/gi, '[aãáàâ]');
normalized = normalized.replace(/[eẽéèê]/gi, '[eẽéèê]');
normalized = normalized.replace(/[iĩíìî]/gi, '[iĩíìî]');
normalized = normalized.replace(/[oõóòô]/gi, '[oõóòô]');
normalized = normalized.replace(/[uũúùû]/gi, '[uũúùû]');
normalized = normalized.replace(/[cç]/gi, '[cç]');
// convert string to a regular expression
// with case insensitive mode
normalized = new RegExp(normalized, 'gi');
// return regular expresion
return normalized;
}
// change 'matcher' method so it became accent insensitive
$.fn.typeahead.Constructor.prototype.matcher = function (item) {
// get item to be evaluated
var source = this.displayText(item);
// make search value case insensitive
var normalized = this.normalize(this.query);
// search for normalized value
return source.match(normalized);
}
// change 'highlighter' method so it became accent insensitive
$.fn.typeahead.Constructor.prototype.highlighter = function (item) {
// get html output
var source = this.displayText(item);
// make search value case insensitive
var normalized = this.normalize(this.query);
// highlight normalized value in bold
return source.replace(normalized, '<strong>$&</strong>');
}
<script src="bootstrap3-typeahead.min.js"></script>
<script src="bootstrap3-typeahead-ci.min.js"></script>
当然,您必须意识到,由于您要替换这两种方法,因此您应该监控 typeahead 中发布的新功能是否不会反射(reflect)在您的自定义方法中。不过,我认为这是一个轻量级且快速的解决方案。
PS.:这是我对 Stack Overflow 的第一个贡献,希望您喜欢!
关于javascript - Typeahead 不敏感的重音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22460272/
我正在寻找一种方法来支持不区分大小写 + 重音不区分搜索的良好性能。到目前为止,我们在使用 MSSql 服务器时没有遇到任何问题,在 Oracle 上我们必须使用 OracleText,而现在我们在
这个问题已经有答案了: Trouble with UTF-8 characters; what I see is not what I stored (5 个回答) 已关闭 5 年前。 我刚刚将一个我
我正在寻找一种在 Linux 中使用反引号 (`)/波形符 (~) 键和其他一些键创建键盘快捷键的方法。在理想情况下: 按下波形符没有任何作用 按下波形符的同时按另一个键会触发(可自定义的)快捷方式
我有一个由术语组成的数组,其中一些包含重音字符。我像这样做一个 preg grep $data= array('Napoléon','Café'); $result = preg_grep('~' .
我使用 TextBox 在 DataGridView 中进行过滤 image .这是完美的工作。表格的单元格包含 1250 个拉丁字符。我想搜索忽略单元格中单词的重音。例子。如果是文本框 "knjaz
我在 Vim 中遇到一个奇怪的映射问题。我使用的是 Azerty 键盘。 在我的 .vimrc 中,我有以下命令可以在段落之间快速移动。 nnoremap _ { vnoremap _ { nnore
我尝试读取一个utf8编码的vcf文件,结果是: { "name": "=4A=61=76=69=65=72=20=4C=75=6A=C3=A1=6E", "tel":
我的数据库中有两个表,info 和 comment,它们的结构如下: info (id(int(10)), name(varchar(80)), ...19 other columns.., phon
我使用 QtWebkit 制作了一个应用程序。在同一个 html 页面中,在 Windows 上使用重音符号(西类牙语)时可以正常工作,但在 Linux (Ubuntu) 上则不起作用。 我不明白为什
我有(例如)两个字符串: $a = "joao"; $b = "joão"; if ( strtoupper($a) == strtoupper($b)) { echo $b; } 我希望它是
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: invalid multibyte char (US-ASCII) with Rails and Ruby
我重写 URL 以包含用户生成的旅游博客的标题。 我这样做是为了 URL 的可读性和 SEO 目的。 http://www.example.com/gallery/280-Gorges_du_Tod
我最近安装了新的 Windows 10 build 14393,我想使用新的 linux 子系统。所以我决定学习 ncurses,但我找不到如何从 getch 中获取带有重音符的字符的 UTF-8 代
我是一名优秀的程序员,十分优秀!