markRegExp() method

Syntax

// javascript
const instance = new Mark(context);
instance.markRegExp(regex[, options]);
// jQuery
$(selector).markRegExp(regex[, options]);

Parameters:

  • regex RegExp - The regular expression. It must have the g flag - the library works with indexes, and only two g and y flags allow setting RegExp lastIndex.

    Note that for backward compatibility, RegExp without the g flag is recompiled internally with this flag.
  • options object - Optional options:

OptionTypeDefaultDescription
elementstring 'mark'A custom mark element e.g. span.
classNamestring '' A custom class to be added to mark elements.
excludestring or string[] []A string or an array of selectors. Specifies DOM elements that should be excluded from searching.

See exclude option for more details.

ignoreGroupsnumber 0The number of adjacent capturing groups that should be ignored from the start of RegExp

e.g. /(\w+)(.)(\w+)(?!\2)/g, ignoreGroups: 2 - mark the group 3

separateGroupsboolean falseWhether to mark RegExp capturing groups instead of whole match

See Highlighting separate groups for more details.

acrossElementsboolean falseWhether to search for matches across elements

See acrossElements option for more details.

wrapAllRangesboolean undefinedMark nesting/overlapping capturing groups

See Marking nesting and overlapping ranges and match groups for more details.

blockElementsBoundaryboolean or object undefinedWhether to limit matches within default HTML block elements and/or custom elements

See Elements boundaries for more details.

  • tagNames string[] - An array of custom HTML tag names
  • extend boolean - true extends default boundary elements by the custom elements otherwise only the custom elements do have boundaries
  • char string - A custom boundary character. The default is \x01.
highlightHighlight undefinedIf a Highlight object is provided, the library switches to using the CSS Custom Highlight API instead of wrapping matches in HTML elements

See highlight option for more details.

highlightNamestring 'advanced-markjs'The name of the Highlight object necessary to register it using HighlightRegistry
staticRangesboolean trueWhether to use StaticRange objects instead of Range objects (Highlight API)

See staticRanges option for more details.

rangeAcrossElementsboolean trueWhether to create a single StaticRange/Range object for matches located across elements (when using the Highlight API with acrossElements option)

See rangeAcrossElements option for more details.

shadowDOMboolean or object undefinedWhether to mark inside shadow DOMs

See shadowDOM option for more details.

iframesboolean or object falseWhether to mark inside iframes

See iframes option for more details.

iframesTimeoutnumber 5000 msThe maximum time to wait for an iframe to load before skipping
debugboolean falseWhether to log messages
logobject consoleLog messages to a specific object
filterfunction A callback to filter matches. It calls for each match (FAE)
filter: (nodeOrArray, matchString, matchesSoFar, info) => {}
  • nodeOrArray Text or Text[] - The text node which includes the match (TAE)
    OR an array of text node(s) which include the match if the Highlight API is used with acrossElements and rangeAcrossElements options
  • matchString string - The matching string:
  1. without ignoreGroups and separateGroups options - the whole match

2. with ignoreGroups option - the match[ignoreGroups+1] group matching string,

e.g. /(-)(\w+)\s+/g, ignoreGroups: 1, the matching string is content of the group 2
3. with separateGroups option - the current group matching string

  • matchesSoFar number - The number of all matches so far
  • info object:
    • match array - The result of RegExp exec() method
    • count number - The number of matches so far MC
    • matchStart boolean - indicate the start of a match AE
    • groupIndex number - The current group index SG
    • abort boolean - Setting it to true breaks the method execution

The function must return either true (highlight) or false (skip highlighting).

eachfunction A callback for each created element OR StaticRange/Range object (Highlight API)
each: (elementOrRange, info) => {}
  • elementOrRange HTMLElement or StaticRange or Range - The marked DOM element OR StaticRange/Range object (Highlight API)
  • info object:
    • match array - The result of RegExp exec() method
    • count number - The number of matches so far MC
    • matchStart boolean - Indicate the start of a match AE
    • groupIndex number - The current index of match group SG
    • groupStart boolean - Indicate the start of group AE SG
    • abort boolean - Setting it to true breaks the method execution
Note: the filter and each callbacks are shared the info object with updated properties.
donefunction A callback on finish.
done: (total, totalMatches) => {}
  • total number - The total number of created HTML elements OR StaticRange/Range objects (Highlight API)
  • totalMatches number - The total number of highlighted matches
noMatchfunction A callback that is called when regex failed to match
noMatch: (regex) => {}
  • regex string - The stringify RegExp
Example with default options values
const options = {
    element: 'mark',
    className: '',
    exclude: [],
    ignoreGroups: 0,
    acrossElements: false,
    wrapAllRanges: false,
    blockElementsBoundary: false,
    
    staticRanges: true, // Highlight API
    rangeAcrossElements: true, // Highlight API
    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 the option acrossElements: true
SG - only available with the option separateGroups: true
AE SG - only available when with options acrossElements: true and separateGroups: true
MC - were already wrapped in HTML elements OR for which were created StaticRange/Range objects
FAE - with the acrossElements option, if the match is located across several elements, it calls for each text node which is part of the match
TAE - with the acrossElements option can be part of the match