With the code as the base, is it possible to highlight any matching items without using an extra plug-in?
I'd like to add style="backgorund: green;"
to the divs where the items were found, so that I can immidiately see them.
The things I've tried so far haven't been working, so I'm hoping some fresh thoughts from outside my brain will do the trick.
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
}
Just another text highlighting jQuery plugin that enables you to highlight any strings within a specific container element using Regular expressions. 1. Download and place the main JavaScript file jquery.highlight.js after jQuery. 2. Highlight numbers using Regex. 3. Highlight characters in the HTML table. 4.
Just another text highlighting jQuery plugin that enables you to highlight any strings within a specific container element using Regular expressions. 1. Download and place the main JavaScript file jquery.highlight.js after jQuery.
jQuery RegEx Examples to use with .match () This is a reference post for some common RegExp ( regular expressions) selectors that can be used with the jQuery.match () function. This is very handy for finding pretty much anything inside your web page text and then doing something neat with it. Also handy for validation on forms.
Combined with jQuery, it allows you to ensure that data sent to the server matches all of your requirements. In this post I have included several example regular expressions that we have used in our web design projects for validating form input.
Why reinvent the wheel? There are ready solutions for this. Try this plugin, for instance. Here's the source code for the plugin. It's literally a few lines of code, so if you really feel like writing your own highlighting engine, you can analyse it to see how the highlighting is performed.
You can try something with a replace()
for (var i = 0; i < needed.length; i++) {
var word = needed[i];
document.body.innerHTML = document.body.innerHTML.replace(word, '<span style="background: #00ff00">' + word + '</span>');
}
Try it here: http://jsfiddle.net/4kjuw/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With