markRegExp() method
Syntax
// javascript
const instance = new Mark(context);
instance.markRegExp(regex[, options]);
// jQuery
$(context).markRegExp(regex[, options]);
Parameters:
regexRegExp - The regular expression. With theacrossElementsoption, it must have thegflag - the library works with indexes, and only twogandyflags allow setting RegExplastIndex.Note that for backward compatibility, RegExp without thegflag is recompiled internally with this flag.Warning: the version 3.x requires thegflag for any RegExp unless it has theyflag.optionsobject - Optional options:
| Option | Type | Default | Description |
|---|---|---|---|
element | string | 'mark' | A custom mark element e.g. span. |
className | string | '' | A custom class to be added to mark elements. |
exclude | string or string[] | [] | A string or an array of selectors. Specifies DOM elements that should be excluded from searching.
See exclude option for more details. |
ignoreGroups | number | 0 | The number of adjacent capturing groups that should be ignored from the start of RegExp
e.g. |
separateGroups | boolean | false | Whether to mark RegExp capturing groups instead of whole match
See Highlighting separate groups for more details. |
acrossElements | boolean | false | Whether to search for matches across elements
See acrossElements option for more details. |
wrapAllRanges | boolean | undefined | Mark nesting/overlapping capturing groups
See Marking nesting and overlapping ranges and match groups for more details. |
blockElementsBoundary | boolean or object | undefined | Whether to limit matches within default HTML block elements and/or custom elements
See Elements boundaries for more details.
|
shadowDOM | boolean | undefined | Whether to mark inside shadow DOMs
See Highlighting in shadow DOM for more details. |
iframes | boolean | false | Whether to mark inside iframes |
iframesTimeout | number | 5000 ms | The maximum time to wait for an iframe to load before skipping |
debug | boolean | false | Whether to log messages |
log | object | console | Log messages to a specific object |
filter | function | A callback to filter matches. It calls for each match (with acrossElements option, if the match is located across several elements, it calls for each text node which is part of the match)
filter: (textNode, matchString, matchesSoFar, filterInfo) => {}
2. with ignoreGroups option - the match[ignoreGroups+1] group matching string,
3. with separateGroups option - the current group matching string
The function must return either | |
each | function | A callback for each marked element
each: (markElement, eachInfo) => {}
| |
done | function | A callback on finish.
done: (totalMarks, totalMatches) => {}
| |
noMatch | function | A callback that is called when regex failed to match
noMatch: (regex) => {}
|
Available properties of the filterInfo object depending on options
| options | match | matchStart | groupIndex | execution | offset |
|---|---|---|---|---|---|
| acrossElements | + | + | - | + | + |
| acrossElements, separateGroups | + | + | + | + | + |
| separateGroups | + | + | + | + | + |
| above options are false | + | - | - | + | + |
Available properties of the eachInfo object depending on options
| options | match | matchStart | groupIndex | groupStart | count |
|---|---|---|---|---|---|
| acrossElements | + | + | - | - | + |
| acrossElements, separateGroups | + | + | + | + | + |
| separateGroups | + | + | + | - | + |
| above options are false | + | - | - | - | + |
Example with default options values
const options = {
element: 'mark',
className: '',
exclude: [],
ignoreGroups: 0,
acrossElements: false,
wrapAllRanges: false,
blockElementsBoundary: false,
shadowDOM: false,
iframes: false,
iframesTimeout: 5000,
filter: (textNode, matchString, matchesSoFar, filterInfo) => {
return true; // must return either true or false
},
each: (markElement, eachInfo) => {},
done: (totalMarks, totalMatches) => {},
noMatch: (regex) => {},
debug: false,
log: window.console
};
JavaScript:
const instance = new Mark(document.querySelector('selector')),
regex = /../gi;
instance.markRegExp(regex, options);
jQuery:
$('selector').markRegExp(regex, options);
• AE - only available with option
acrossElements: true• SG - only available with option
separateGroups: true• AE SG - only available when with options
acrossElements: true and separateGroups: true