将$(this).text().match(r)写入香草JS的正确方式



我用jQuery编写了以下函数,我想将其转换为javascript,但到目前为止我找不到合适的方法。

const word = document.getElementById("searchField").value;
const r = new RegExp("(" + word + ")", "ig");
$(".list-item").each(function (i) {
if ($(this).text().match(r)) {

}
});

我是这样改写的:

const word = document.getElementById("searchField").value;
const r = new RegExp("(" + word + ")", "ig");
let pickComp = document.querySelectorAll('.list-item');
Array.from(pickComp).forEach((i) => {
if (//how can I rewrite the jQuery here?) {

}
})

const word = document.getElementById("searchField").value;
const r = new RegExp("(" + word + ")", "ig");
const pickComp = document.querySelectorAll('.list-item');
pickComp.forEach(item => {
if (item.innerHTML.match(r)) {
console.log("Match!!");
}
})
<p class="list-item">abc</p>
<p class="list-item">abc</p>
<p class="list-item">abc</p>
<p class="list-item">abc</p>
<input id="searchField" value="abc">

最新更新