Filtering matches
To filter RegExp capturing groups see: Filtering capturing groups.
To filter matches in the mark() method with acrossElements option
See mark() filter callback's info object property.
let count = 0;
instance.mark('AB', {
'acrossElements': true,
'filter': (textNode, term, matchesSoFar, termMatchesSoFar, info) => {
// to mark only the first match
info.execution.abort = true; return true;
// the filter callback requires its own match counter
if (info.matchStart) {
count++;
}
// mark the first 10 matches.
if (count > 10) { info.execution.abort = true; return false; }
// skip between
if (count > 10 && count < 20) { return false; }
// mark between
if (count <= 10) { return false; }
else if (count > 20) { info.execution.abort = true; return false; }
return true;
}
});
To filter matches in the mark() method without acrossElements option
let count = 0;
instance.mark('AB', {
'filter': (textNode, term, matchesSoFar, termMatchesSoFar, info) => {
// the only difference is counter implementation
count++;
}
});
To filter matches in the markRegExp() method
See markRegExp() filter callback's info object property.
let count = 0, reg = /.../gi;
// if you have access to the RegExp object with 'acrossElements' option, you can
// also used 'reg.lastIndex = Infinity;' instead of 'info.execution.abort = true;'
instance.markRegExp(reg, {
'filter': (textNode, matchString, matchesSoFar, info) => {
// to mark only the first match
info.execution.abort = true; return true;
// the filter callback requires its own match counter
if (info.matchStart) {
count++;
}
// mark the first 10 matches.
if (count > 10) {
info.execution.abort = true;
return false;
}
// skip between
if (count > 10 && count < 20) { return false; }
// mark between
if (count <= 10) { return false; }
else if (count > 20) {
info.execution.abort = true;
return false;
}
return true;
},
});
Mark the first desired number of matches on each callback with acrossElements option.
let reg = /.../gi;
instance.markRegExp(reg, {
'acrossElements': true,
'each': (markElement, info) => {
// to mark only the first match
reg.lastIndex = Infinity;
// first 10 matches
if (info.count >= 10) {
reg.lastIndex = Infinity;
}
}
});