markRegExp() method
Syntax
// javascript
const instance = new Mark(context);
instance.markRegExp(regex[, options]);
// jQuery
$(context).markRegExp(regex[, options]);
Parameters:
regex
RegExp - The regular expression. WithacrossElements
option it must haveg
flag - it works with indexes and only twog
andy
flags allow control RegExplastIndex
.
Note that for backward compatibility, RegExp withoutg
flag is recompile internally withg
flag.
Although withoutacrossElements
option it doesn't requireg
flag, it still recommended having this flag for future changes.options
object - 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 max time to wait for iframe(s) 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) => {}
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 when
acrossElements
option is set totrue
- SG - only available when
separateGroups
option is set totrue
- AE SG - only available when both
acrossElements
andseparateGroups
options are set totrue