Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/angezid/advanced-mark.js/llms.txt

Use this file to discover all available pages before exploring further.

markRegExp() highlights matches for a custom regular expression. The regex must have the g flag; for backward compatibility it is recompiled internally with the g flag if missing. This method is ideal when you need pattern-based matching beyond what mark() supports — for example matching HTML tags, structured tokens, or capturing specific groups independently.

Syntax

// Vanilla JS
const instance = new Mark(context);
instance.markRegExp(regexp[, options]);

// jQuery
$(selector).markRegExp(regexp[, options]);

Parameters

regexp
RegExp
required
The regular expression to match against the context text. Must include the g flag — the library manipulates lastIndex internally and requires it. If the g flag is absent it is recompiled automatically (backward-compatibility shim).When using separateGroups, the d flag is also required to enable match indices (hasIndices).
// Correct: g flag present
const regex = /\b\w{5,}\b/gi;

// separateGroups requires g + d flags
const groupRegex = /(\w+)\s+(\w+)/gd;
options
object
Optional configuration object.

Default Options

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,
};

Usage Examples

const instance = new Mark(document.querySelector('.content'));

// Highlight 5+ letter words, case-insensitive
instance.markRegExp(/\b\w{5,}\b/gi);
AE — property only available when acrossElements: true. SG — property only available when separateGroups: true. AE SG — only available when both options are enabled. MC — count reflects matches already wrapped in HTML elements or registered as Highlight API ranges at the time the callback fires.
The separateGroups option requires the d (hasIndices) flag on your regex. Without it, group boundary positions cannot be determined and the option will not function correctly.

Build docs developers (and LLMs) love