JSFiddle - React, Tailwind, and code Playground
HTML
Phasellus €porttitor nunc augue sagittis massa tincidunt nec parturient? Porta montes porta turpis tincidunt sociis et eros! Diam tempor! Elementum? Est placerat in risus! In? Tempor sit, scelerisque est magna. Vel nascetur turpis t€ristique purus tortor amet, vut augue habitasse, adipiscing, nec! Quis aliquam rhoncus pulvinar phasellus ac. Et integer in natoque diam augue integer integer. Mauris, urna integer eros duis, duis magnis! Ut proin auctor cursus porta magna vel et et adipiscing auctor integer, tristiq€ue aliquam augue. Aliquam, sit. Elementum pid diam ridiculus amet! Ac tincidunt amet amet purus, duis urna nec, augue! Elementum tristique! Turpis et magna, habitasse, al€iquet odio sed tristique nunc, diam od€io augue, a natoque velit turpis lorem dignissim ultricies elementum facilisis egestas? Mus diam.
Nec lundium et! Amet, habitasse proin aenean, urna, tincidunt, phasellus a, pid aliquam, ut risus, in dolor, urna enim, porta auctor amet ac. Nascetur montes tincidunt adipiscing sed hac urna porta parturient! Ultrices scelerisque pulvinar. Massa pulvinar aliquam turpis? Pulvinar magnis adipiscing et porttitor urna aenean! Ridiculus. Sed tincidunt, nec, tincidunt dis in ultricies, platea cum? Aenean in non, et pellentesque, vel a. Mattis auctor etiam lacus! Aenean aliquet pid pellentesque, dis lectus enim dapibus, natoque, egestas cras mauris! Lectus, sagittis turpis odio sociis nisi sagittis pellentesque in elementum, eu, adipiscing amet parturient dapibus, egestas scelerisque in pulvinar tempor integer enim phasellus amet. Cursus nunc diam odio ridiculus sagittis lacus porttitor urna etiam, egestas augue turpis, egestas vel magna.
Aenean elit, vel, tristique amet, lorem porttitor adipiscing aliquam. Tempor nunc, tortor nunc dis natoque. Massa ac sit turpis ut, pid risus, nunc? Enim nisi, rhoncus porttitor lacus natoque! Mauris ac? Velit rhoncus in aliquet amet pulvinar lectus magnis. Velit vut, nascetur proin eros placerat turpis porttitor,...
JavaScript
(function($){
$.fn.highlightText = function () {
// handler first parameter
// is the first parameter a regexp?
var re,
hClass,
reStr,
argType = $.type(arguments[0]),
defaultTagName = $.fn.highlightText.defaultTagName;
if ( argType === "regexp" ) {
// first argument is a regular expression
re = arguments[0];
}
// is the first parameter an array?
else if ( argType === "array" ) {
// first argument is an array, generate
// regular expression string for later use
reStr = arguments[0].join("|");
}
// is the first parameter a string?
else if ( argType === "string" ) {
// store string in regular expression string
// for later use
reStr = arguments[0];
}
// else, return out and do nothing because this
// argument is required.
else {
return;
}
// the second parameter is optional, however,
// it must be a string or boolean value. If it is
// a string, it will be used as the highlight class.
// If it is a boolean value and equal to true, it
// will be used as the third parameter and the highlight
// class will default to "highlight". If it is undefined,
// the highlight class will default to "highlight" and
// the third parameter will default to false, allowing
// the plugin to match partial matches.
// ** The exception is if the first parameter is a regular
// expression, the third parameter will be ignored.
argType = $.type(arguments[1]);
if ( argType === "string" ) {
hClass = arguments[1];
}
else if ( argType === "boolean" ) {
hClass = "highlight";
if ( reStr ) {
reStr = "\\b" + reStr + "\\b";
}
}
else {
hClass = "highlight";
}
if ( arguments[2] && reStr ) {
reStr = reStr = "\\b" + reStr + "\\b";
}
// if re is not defined ( which means either an array or
// string was passed as the first parameter ) create the
// regular expression.
if (!re) {
re = new RegExp( "(" + reStr + ")", "ig" );
}
// iterate through...