如何在replace
函數中使用動態變量作爲第一個參數?使用變量替換所有出現的特定字符串
我有這樣的代碼,當用戶搜索指定的字符串:
var query = jQuery.trim(jQuery(this).val()).toLowerCase();
console.log(query + ' was searched')
jQuery('.one-reference').each(function() {
var jQuerythis = jQuery(this);
if (jQuerythis.text().toLowerCase().indexOf(query) === -1) {
jQuerythis.fadeOut();
}
else {
jQuerythis.html(jQuerythis.html().replace(/&/g, '<strong>$&</strong>'));
jQuerythis.fadeIn();
}
});
這replace(/&/g, '<strong>$&</strong>'))
不工作。
我想用<strong>
標籤覆蓋所有出現的query
。
你能創建一個演示問題的小提琴嗎? – Anubhav
難道你不應該搜索'query'而不是'&'嗎? – Stefan
@Stefan它將搜索sting'query'然後...不是變量中的字符串'它會搜索' – Umair