[英]Modify code to wrap whole words & make it non case sensitive
function filterSearchWords() {
var query = $("#searchquery").val();
go(query);
function go(query) {
$(".search-result p:contains(" + query + ")").html(function (_, html) {
return html.replace(query, "<span class='search-enlight'>$&</span>", 'gi');
});
$(".search-result h2 a:contains(" + query + ")").html(function (_, html) {
return html.replace(query, "<span class='search-enlight'>$&</span>", 'gi');
});
console.log(query);
}
}
這是我的嘗試。 它會增加跨度,但只能添加到包含字符串的單詞或單詞的一部分。 可以說字符串是Pie,它將跨度添加到所有“ Pie”並在Piechart中包裝“ Pie”,但是我希望它包裝所有Piechart,並且不帶大寫字母的pie。
任何人都可以修改我的代碼來幫助我嗎?
謝謝
我嘗試使用正則表達式“ gi”似乎不起作用:/
更新快到了...
function filterSearchWords() {
var query = $("#searchquery").val();
go(query);
function go(query) {
$.extend($.expr[':'], {
'containsi': function (elem, i, match, array) {
return (elem.textContent || elem.innerText || '').toLowerCase()
.indexOf((match[3] || "").toLowerCase()) >= 0;
}
});
$(".search-result p:containsi(" + query + ")").html(function (_, html) {
return html.replace(new RegExp(query, "gi"), "<span class='search-enlight'>$&</span>");
});
$(".search-result h2 a:containsi(" + query + ")").html(function (_, html) {
return html.replace(new RegExp(query, "gi"), "<span class='search-enlight'>$&</span>");
});
console.log(query);
}
}
這是我的最后一個問題。 我已經成功刪除了區分大小寫的部分,但它與整個單詞不匹配。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.