http://jsfiddle.net/nHGU6/
测试HTML:
<div id="wrapper-accent-sensitive">
<p>cafe</p>
<p>asdf</p>
<p>café</p>
</div>
<hr />
<div id="wrapper-not-accent-sensitive">>
<p>cafe</p>
<p>asdf</p>
<p>café</p>
</div>
测试CSS:
.yellow {
background-color: #ffff00;
}
更换的Javascript:
jQuery.fn.highlight = function (words, options) {
var accentedForms = {
'c': 'ç',
'e': 'é'
};
var settings = { className: 'highlight', element: 'span', caseSensitive: false, wordsOnly: false, accentInsensitive: false };
jQuery.extend(settings, options);
if (settings.accentInsensitive) {
for (var s in accentedForms) {
words = words.replace(s, '[' + s + accentedForms[s] + ']');
}
}
if (words.constructor === String) {
words = [words];
}
var flag = settings.caseSensitive ? "" : "i";
var pattern = "(" + words.join("|") + ")";
if (settings.wordsOnly) {
pattern = "\\b" + pattern + "\\b";
}
var re = new RegExp(pattern, flag);
return this.each(function() {
jQuery.highlight(this, re, settings.element, settings.className);
});
};
测试代码:
$(document).ready(function() {
$("#wrapper-accent-sensitive").highlight("cafe", { className: 'yellow' });
$("#wrapper-not-accent-sensitive").highlight("cafe", { className: 'yellow', accentInsensitive: true });
});
查看来自casablanca的答案:http://stackoverflow.com/questions/4261740/accent-insensitive-regex。基本上,围绕jquery.highlight.js的第91行进行修改,以便正则表达式现在包含字符类。可能会在第83行附近添加“accentInsensitive”选项。 – anon 2011-05-20 04:16:35
谢谢,但是我对如何在代码中实现该代码感到有点遗憾... – Santiago 2011-05-20 21:28:39
好的。我在下面添加了一个实现。 – anon 2011-05-21 01:23:04