以代码为基础,是否可以在不使用额外插件的情况下突出显示任何匹配项目?如何突出RegEx与jQuery的匹配?
我想将style="backgorund: green;"
添加到发现物品的div中,以便我可以直接看到它们。
到目前为止我尝试过的东西都没有工作,所以我希望从我的大脑外面一些新鲜的想法能够做到这一点。
function finder(items){
var needed = [
/* items */
];
var re = new RegExp(needed.join("|"), "i");
return(items.match(re) != null);
}
var found = finder(document.body.innerHTML);
var output = found ? "found" : "not found";
if(output == 'found') {
//highlight found array item
}
相关:我最近做了类似的事情:https://github.com/Ralt/regexfiddle –
你想添加样式到文本的div,或者创建一个围绕文本发现的范围只突出显示? –
@FlorianMargaine我想将样式添加到包含查找的元素,而不是在其周围创建新元素。现在看看现在给出的答案,看看是否有我可以使用的东西。 – spiel