I basically need to highlight a particular word in a block of text. For example, pretend I wanted to highlight the word "dolor" in this text:
I have created a repository on similar concept that changes the colors of the texts whose colors are recognised by html5 (we don't have to use actual #rrggbb values and could just use the names as html5 standardised about 140 of them)
colors.js
$( document ).ready(function() {
function hiliter(word, element) {
var rgxp = new RegExp("\\b" + word + "\\b" , 'gi'); // g modifier for global and i for case insensitive
var repl = '' + word + '';
element.innerHTML = element.innerHTML.replace(rgxp, repl);
};
hiliter('dolor', document.getElementById('dolor'));
});
.myClass{
background-color:red;
}
highlight
Lorem ipsum dolor sit amet, consectetuer adipiscing elit.
Quisque bibendum sem ut lacus. Integer dolor ullamcorper libero.
Aliquam rhoncus eros at augue. Suspendisse vitae mauris.